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, andDiffDeletehighlights
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 fromstartinstead - Navigate hunks with
]c(next) and[c(previous) after refreshing - Apply or obtain a hunk with
dp(diff put) ordo(diff obtain) after updating - In Neovim,
diffmode integrates with thediffoptoption;:set diffopt+=linematch:60can improve accuracy