How do I compare two files side by side using Vim's built-in diff mode?
Answer
:vertical diffsplit {file}
Explanation
Vim has a built-in diff mode that highlights added, removed, and changed lines between two (or more) buffers. :vertical diffsplit opens a second file in a vertical split alongside the current buffer and enables diff highlighting for both.
How it works
:vertical diffsplit {file}splits the window vertically and opens{file}in diff mode with the current buffer- Changed lines are highlighted;
foldscollapse identical sections so only differences are visible - Navigate between hunks with
]c(next change) and[c(previous change) :diffupdaterefreshes the diff highlighting after making edits:diffoff(or:diffoff!for all windows) exits diff mode
You can also start Vim in diff mode from the shell:
vim -d file1 file2
Example
You have config.prod.yaml open and want to compare it with config.dev.yaml:
:vertical diffsplit config.dev.yaml
Both files appear side by side with differences highlighted. Press ]c to jump to the first differing section.
Tips
:diffget(do) pulls a change from the other buffer into the current one:diffput(dp) pushes a change from the current buffer to the other one:windo diffthisenables diff mode for all currently open windows — useful when you have already opened both files manually- For a horizontal split instead, use
:diffsplit {file}(withoutvertical)