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

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

  • diffopt controls how Vim computes and renders diffs
  • algorithm:histogram uses a matching strategy that handles moved or repeated lines more gracefully than the default in many real codebases
  • indent-heuristic biases hunk boundaries toward indentation structure, which usually makes function/block changes easier to read
  • :set diffopt+=... appends these flags without overwriting your existing diffopt customizations

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.vim if you frequently work in diff mode
  • If a repo has unusual formatting, compare results with algorithm:patience as an alternative
  • Use :set diffopt? before and after to confirm exactly which flags are active

Next

How do I keep a utility split from resizing when other windows open or close?