Real-time vimdiff Without Saving Files to Compare Clipboard Content

  • 13th Mar 2025
  • 1 min read

📝 From my notes: living personal cheatsheets.

I often want to compare a few lines of text without saving them to files.

Here’s a command that creates a two-pane vimdiff where you can paste text and see differences highlighted in real-time:

vim -c "set noswapfile" \
    -c "vnew" \
    -c "windo setlocal nobuflisted buftype=nofile noswapfile" \
    -c "set diffopt=filler,iwhite" \
    -c "windo diffthis" \
    -c "wincmd h" \
    -c "autocmd TextChanged,TextChangedI * diffupdate"

Here’s what each part does:

  • set noswapfile disables vim’s swap files for these buffers
  • vnew creates a new vertical split window
  • windo setlocal nobuflisted buftype=nofile noswapfile sets both windows to not appear in buffer list and not be associated with files
  • set diffopt=filler,iwhite configures diff to show filler lines and ignore whitespace differences
  • windo diffthis activates diff mode in both windows
  • wincmd h moves cursor to the left window
  • autocmd TextChanged,TextChangedI * diffupdate automatically updates diff highlighting whenever text changes. Otherwise you’d need to manually run :diffupdate

I added it as an alias to my shell configuration:

alias dp='vim -c "set noswapfile" -c "vnew" -c "windo setlocal nobuflisted buftype=nofile noswapfile" -c "set diffopt=filler,iwhite" -c "windo diffthis" -c "wincmd h" -c "autocmd TextChanged,TextChangedI * diffupdate"'

Now I can run dp (for diff paste) and get instant diffing.


Extra: VS Code equivalent

There’s an equivalent feature in Visual Studio Code: press ⌘+Shift+P to open the command palette and search for “File: Compare New Untitled Text Files”.

Search for workbench.files.action.compareNewUntitledTextFiles to set up a keybinding for it.