How do I diff two buffers side by side in Vim?
Answer
:windo diffthis
Explanation
The :windo diffthis command activates Vim's built-in diff mode across all visible windows, highlighting the differences between them. This is incredibly useful for comparing two versions of a file, two config files, or any two pieces of text without leaving Vim.
How it works
:windoexecutes a command in every visible window:diffthistells Vim to include the current window's buffer in a diff comparison- When two or more windows have
:diffthisactive, Vim highlights additions, deletions, and changes between them with colors and fold markers
Step-by-step workflow
- Open the first file or paste your first block of text
- Run
:vsplitto create a vertical split (or:splitfor horizontal) - Open the second file with
:edit other-file.txtor paste the second block of text - Run
:windo diffthisto activate diff mode in both windows
Vim now shows the two buffers side by side with differences highlighted. The windows scroll together, and folded regions hide identical lines so you can focus on the changes.
Navigating diffs
]c— jump to the next change[c— jump to the previous changedo— obtain the change from the other buffer into the current one (diff obtain)dp— push the change from the current buffer to the other one (diff put)
Turning off diff mode
To exit diff mode, run:
:windo diffoff
Tips
- Use
:diffupdateto refresh the diff after making edits - Use
vim -d file1 file2from the command line to open Vim directly in diff mode - You can also diff three or more files by opening three splits and running
:windo diffthis - For comparing text from your clipboard, open a new buffer with
:vnew, paste the content, then run:windo diffthis - Diff mode automatically enables
scrollbindandcursorbindso the two windows scroll together - Use
:set diffopt+=iwhiteto ignore whitespace differences