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

How do I make Vim's diff mode produce cleaner, more readable diffs?

Answer

:set diffopt+=algorithm:patience,indent-heuristic

Explanation

By default, Vim uses a basic longest-common-subsequence diff algorithm that can produce noisy, hard-to-read diffs. Switching to the patience algorithm (or histogram) produces diffs that better align with how developers think about code changes — grouping related blocks together and avoiding spurious matches on common tokens like braces.

How it works

  • diffopt controls how Vim displays diffs in :windo diffthis or vim -d mode
  • algorithm:patience uses the patience diff algorithm (also default in Git since 2010)
  • algorithm:histogram is an evolution of patience with better performance on large files
  • indent-heuristic shifts diff boundaries to prefer indented block boundaries, making diffs align with code structure rather than line similarity

Example

Add to your ~/.vimrc or init.vim:

set diffopt+=algorithm:patience,indent-heuristic

Or also suppress whitespace differences:

set diffopt+=algorithm:histogram,indent-heuristic,iwhite

Tips

  • iwhite ignores trailing whitespace differences (useful for mixed-indent files)
  • iblank ignores completely blank line differences
  • context:5 changes the number of context lines shown around each hunk (default is 6)
  • vertical forces vertical splits for diff mode instead of horizontal
  • Check current settings with :set diffopt?
  • Requires Vim 8.1+ or Neovim for the algorithm key

Next

How do I refer to the matched text in a Vim substitution replacement?