vim has this functionality built in (with the correct command line flag).
vim -d <file1> <file2>
This opens each file in a view and highlights the differences.
Any code that is identical is folded
away so you do not need to look at identical code or scroll through huge chunks of identical code.
But there is also a wrapper application vimdiff
that correctly invokes vim with the correct flags.
vimdiff source1.cpp source2.cpp
If you are using git you can set up an external diff tool. So it is easy to set up vimdiff to be the diff tool for git.
git config --global diff.tool vimdiff
When using vimdiff you can edit either side and diff highlighting keeps pace to show you the differences.
Note: When editing from a git diff. If you try and edit the repository stored version of the file your changes will be discarded when you exit (git does not trust you with the original so you are diffing against a tmp copy) but you can edit the local copy to your hearts content and save it over you current version.
Some basic commands that are useful in vimdiff
dp diffput: puts changes under the cursor into the other file
making them identical (thus removing the diff).
do diffget: (o => obtain). The change under the cursor is replaced
by the content of the other file making them identical.
]c Jump to the next diff
[c Jump to the previous diff
Other vim settings I use to work with highliting with vimdiff
if &diff
highlight! link DiffText MatchParen
endif
This turns off highlighting on the bits of code that are changed. So the line that is changed is highlighted so I can spot the changes, but the actual text that has changed stands out on the line (as it is not highlighted).
:Gdiff
and:Gvdiff
both commands lets you see the side by side diff of the current buffer if your current buffer is managed by git. It also heps you to resolve conflicts in a three window layout when you have merge conflicts on some filesvd () { diff $@ > /dev/null ; if [[ $? -eq 1 ]] ; then ; vimdiff -c 'windo set syntax=off' $@ ; fi ; }
and I invoke it withvd file1 file2
. It usesdiff
to determine whether the files differ and only opensvimdiff
if that is so. Otherwise, I stay in the shell. I also disable syntax highlighting in Vim because I find it distracting when diffing. Only works with two files.mergetool
- see stackoverflow.com/a/7313949/327074