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

How do I compare the current buffer to another file using Vim's built-in diff mode without leaving the editor?

Answer

:diffsplit {file}

Explanation

:diffsplit {file} opens a file in a horizontal split and immediately activates diff mode, highlighting the differences between both buffers. It combines :split and :diffthis into a single command, making file comparison fast and seamless without leaving Vim.

How it works

  • :diffsplit {file} — open {file} in a horizontal split and enable diff highlighting on both windows
  • :vert diffsplit {file} — use a vertical (side-by-side) split, which is usually more readable on wide terminals
  • Vim uses the diff, diffadd, and diffchange highlight groups to color the changed regions

Navigating the diff

Once in diff mode, these commands let you work through the changes:

  • ]c — jump to the next diff hunk
  • [c — jump to the previous diff hunk
  • dpdiff put: push the current hunk from this window to the other
  • dodiff obtain: pull the change from the other window into this one
  • :diffupdate — re-diff both buffers after making manual edits

Example

Compare a configuration file against a known-good backup:

:vert diffsplit ~/.config/nvim/init.vim.bak

Vim opens a side-by-side view with changed lines highlighted and ]c / [c ready to jump between differences.

Tips

  • Exit diff mode with :diffoff! to restore normal display across all windows
  • For three-way merges, open three splits and run :diffthis in each manually
  • Starting from the shell, vimdiff file1 file2 is equivalent to opening the first file and running :vert diffsplit file2

Next

How do I prepend a value to the front of a comma-separated Vim option like path or runtimepath?