How do I improve Vim diff readability for moved or reindented code blocks?
Answer
:set diffopt+=algorithm:histogram,indent-heuristic
Explanation
Default diff behavior can produce noisy hunks when code is moved or indentation changes significantly. Adding histogram matching plus indentation heuristics often yields cleaner block alignment, especially in refactors where semantic structure stayed similar but line positions changed. This is a high-leverage setting when you review large patches directly inside Vim.
How it works
diffoptcontrols how Vim computes and renders diffsalgorithm:histogramuses a matching strategy that handles moved or repeated lines more gracefully than the default in many real codebasesindent-heuristicbiases hunk boundaries toward indentation structure, which usually makes function/block changes easier to read:set diffopt+=...appends these flags without overwriting your existingdiffoptcustomizations
Example
Suppose a function body is reindented and one inner block moves. With default settings, you may see fragmented hunks that split related lines apart. After running:
:set diffopt+=algorithm:histogram,indent-heuristic
Vim often groups logically related changes into larger, cleaner hunks, reducing visual noise while preserving exact line-level diff data.
Tips
- Put the setting in your
vimrc/init.vimif you frequently work in diff mode - If a repo has unusual formatting, compare results with
algorithm:patienceas an alternative - Use
:set diffopt?before and after to confirm exactly which flags are active