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
diffoptcontrols how Vim displays diffs in:windo diffthisorvim -dmodealgorithm:patienceuses the patience diff algorithm (also default in Git since 2010)algorithm:histogramis an evolution of patience with better performance on large filesindent-heuristicshifts 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
iwhiteignores trailing whitespace differences (useful for mixed-indent files)iblankignores completely blank line differencescontext:5changes the number of context lines shown around each hunk (default is 6)verticalforces vertical splits for diff mode instead of horizontal- Check current settings with
:set diffopt? - Requires Vim 8.1+ or Neovim for the
algorithmkey