How do I diff two files that are already open in Vim splits?
Answer
:diffthis
Explanation
You often have two files open side by side and want to compare them without leaving Vim or launching vimdiff. The :diffthis command turns the current window into a diff participant. Run it in each of two split windows, and Vim instantly highlights the differences between them with full diff navigation.
How it works
:diffthis— marks the current window as part of a diff. Run this in each window you want to compare- Once two or more windows are marked, Vim activates diff mode: color-coded change highlighting, folded unchanged regions, and synchronized scrolling
:diffoff— removes diff mode from the current window:diffoff!— removes diff mode from all windows
Example
With two files open in vertical splits:
" In the left window:
:diffthis
" Switch to the right window:
<C-w>l
:diffthis
Vim immediately shows additions, deletions, and modifications highlighted. Navigate between changes with ]c (next change) and [c (previous change).
Tips
- Use
:diffget(do) and:diffput(dp) to merge individual changes between windows :diffupdateforces Vim to recalculate the diff after manual edits- This also works for comparing a buffer against a scratch buffer — paste clipboard content into a new split, then diff both
- For three-way merges, you can run
:diffthisin three windows simultaneously