How do I make Vim diff ignore whitespace and use patience algorithm for clearer hunks?
Answer
:set diffopt+=iwhite,algorithm:patience
Explanation
When whitespace-only churn and noisy line matching make diffs hard to review, tuning diffopt can dramatically improve signal. Adding iwhite tells Vim to ignore whitespace differences, while algorithm:patience improves hunk alignment for reordered or refactored code. This is especially helpful in large formatting commits where default diff output becomes fragmented.
How it works
:set diffopt+=iwhite,algorithm:patience
:set diffopt+=...appends settings without removing your existingdiffoptvaluesiwhiteignores whitespace-only differences in diff modealgorithm:patienceswitches to a diff strategy that tends to produce more stable hunks on source code
Because this modifies an option, use :setlocal if you only want it in the current window, or put the setting in your vimrc for a persistent default.
Example
Imagine a file where indentation changed and a helper block moved:
- if (ready) {
- run_task();
- }
+if (ready) {
+ run_task();
+}
... helper function moved lower in file ...
With default diff settings, Vim may show extra churn from indentation and split movement into less useful hunks. After enabling iwhite,algorithm:patience, the diff focuses more on semantic edits and often groups moved logic into clearer chunks.
Tips
- Check active values with
:set diffopt? - Revert quickly with
:set diffopt-=iwhite diffopt-=algorithm:patience - Combine with
:diffupdateafter changing options in an existing diff session