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

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

  • :windo executes a command in every visible window
  • :diffthis tells Vim to include the current window's buffer in a diff comparison
  • When two or more windows have :diffthis active, Vim highlights additions, deletions, and changes between them with colors and fold markers

Step-by-step workflow

  1. Open the first file or paste your first block of text
  2. Run :vsplit to create a vertical split (or :split for horizontal)
  3. Open the second file with :edit other-file.txt or paste the second block of text
  4. Run :windo diffthis to 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 change
  • do — 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 :diffupdate to refresh the diff after making edits
  • Use vim -d file1 file2 from 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 scrollbind and cursorbind so the two windows scroll together
  • Use :set diffopt+=iwhite to ignore whitespace differences

Next

How do I edit multiple lines at once using multiple cursors in Vim?