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

How do I refresh the diff highlighting in Vim when it becomes stale after editing?

Answer

:diffupdate

Explanation

After making edits in a vimdiff session, the diff highlighting can become out of sync with the actual content — showing incorrect change markers or missing hunks. :diffupdate (short: :diffu) forces Vim to recompute the diff and redraw all diff highlights in the current window layout.

How it works

  • :diffupdate — recompute the diff for all windows in diff mode without reloading files
  • :diffupdate! — same as above, but also reload each file from disk first (useful if a file was changed externally)
  • The command recalculates which lines are added, deleted, or changed, and updates all DiffAdd, DiffChange, and DiffDelete highlights

Example

Open two files for comparison, make an edit, and refresh:

:vertical diffsplit other.txt
" ... make some edits ...
:diffupdate

The diff markers now accurately reflect the current buffer state.

Tips

  • Use :diffupdate! when working with externally modified files (e.g., after a Git operation in a terminal)
  • If syntax highlighting (not diff markers) is broken, use :syntax sync fromstart instead
  • Navigate hunks with ]c (next) and [c (previous) after refreshing
  • Apply or obtain a hunk with dp (diff put) or do (diff obtain) after updating
  • In Neovim, diff mode integrates with the diffopt option; :set diffopt+=linematch:60 can improve accuracy

Next

How do I make Vim's indent commands always align to a clean shiftwidth boundary?