How do I tune diffopt so diffs align moved code and produce cleaner hunks?
Answer
:set diffopt+=algorithm:patience,indent-heuristic,linematch:60
Explanation
Default diff settings can produce noisy hunks when code is moved, re-indented, or lightly refactored. This diffopt combination improves matching quality so related lines stay aligned and the review signal is much clearer. It is especially useful for large code moves where default diffs look like full rewrites.
How it works
algorithm:patienceprefers unique anchors, usually producing more stable hunks on source codeindent-heuristicimproves hunk boundaries by considering indentation structurelinematch:60enables inline line matching for changed blocks up to a threshold, helping detect moved or partially edited lines:set diffopt+=...appends these behaviors without resetting your existing diff options
Example
In a refactor where functions are reordered and lightly edited, default diff often marks entire regions as replaced. After setting:
:set diffopt+=algorithm:patience,indent-heuristic,linematch:60
:diffupdate
you typically get smaller, more accurate hunks and clearer inline correspondence between old and new code.
Tips
- Use
:set diffopt?to inspect your current active flags - If performance drops on huge files, reduce or remove
linematch:60 - Put this in filetype-specific autocmds if you only want it for certain languages