vimtricks.wiki Concise Vim tricks, one at a time.

How do I compare two files side by side using diff mode?

Answer

:diffthis in both windows

Explanation

The :diffthis command enables diff mode for the current buffer. Running it in two windows shows their differences highlighted.

How it works

  1. Open two files in splits: :vsplit other_file
  2. In the first window: :diffthis
  3. Switch to the second window: <C-w>w
  4. In the second window: :diffthis
  5. Differences are now highlighted

Example

:vsplit file2.txt
:diffthis
<C-w>w
:diffthis

Tips

  • :diffsplit filename opens a file and enables diff in one step
  • ]c and [c jump between diff hunks
  • do (diff obtain) pulls changes from the other window
  • dp (diff put) pushes changes to the other window
  • :diffoff disables diff mode
  • :diffupdate refreshes after manual edits

Next

How do you yank a single word into a named register?