Kurt Keller's Blog

tech notes and more

diff

There are numerous nice diff and merge tools out there such as meld, beediff, xxdiff and others. But they often require a graphical environment or they are not installed. Don’t despair, vim does it too.

When you have two files to compare, add both files on the command line when starting vim and either use the command line parameter -d or use vimdiff for the command instead of vim. You’ll then see a screen vertically split into two halves, each half showing one of the two files and the differences highlighted. To save space, long parts which are the same in both files will be hidden behind folds.

Instead of typing the full path to the second file, it is also possible to just give the directory in which the second file is and vim will automatically search there for a file named the same as the first file. And it is also possible to compare more than two files.

In diff mode, use ]c and [c respectively to jump to the next/last difference. To merge a change from the other file, go to the difference in question and use :diffget or :do to get/obtain the version from the other file. To merge a change to the other file, go to the difference in question and use :diffput or :dp to put the version from this file to the other file.

Simple as this, all in your favourite editor and without the need for a graphical environment (though it does work in gvim too).

vimdiff <file1> <file2> vim -d <file1> <file2> start vim in diff mode with file1 and file2 being compared
]c move to the next difference
[c move to the previous difference
:do :diffget get the version from the other file into this file
:dp :diffput push the version from this file into the other file
Share

Leave a Reply