Diff and merge using vim (or gvim)

vimdiff allows you to diff and merge two files in vim. Start it with either of the following commands:

vimdiff [file1] [file2]
vim -d [file1] [file2]

You can also use gvim, in which case substitute "vim" for "gvim" in the above commands.

If you are already in vim (editing [file1]), use the following command to do a vertical diff between the current file and another.

:vert diffsplit [file2]

If you want to use the --remote-tab or --remote-tab-silent switch to open the diff in a new tab in the existing window, you need to start the diff via a command rather than via a switch:

vim --remote-tab-silent -c "vert diffsplit [file2]" [file1]

Note that you will need to escape any spaces in [file2].


To get both files scrolling at the same time I had to set the scrollbind option in my .vimrc file (or set it in /etc/vim/vimrc as a global setting):

set scrollbind

Redoing the diff

If you loose the diff for whatever reason (e.g. I lost the diff when opening a new tab with :tabnew), turn the diff option on again.

:set diff

You'll also need to redo the diff if you make a non-trivial change to a line, in which case run the following:


Moving around

Switch viewport:

ctrl-w w

Jump to next diff:


Jump to previous diff:



Whole diff

To replace a diff in the current viewport with that from the other viewport, use the :diffget command. This can be shortened to the following:


To replace a diff in the other viewport with that from the current viewport, use the :diffput command. This can be shortened to the following:


If you want to get contents from the other viewport when the current viewport has "filler" - i.e. a series of dashes - then do so from the line below.

Part of the diff

To replace just a single line or multiple lines in a diff in the current viewport (rather than getting the entire diff), use a range. You have to use the full command for this - you can't use the shortened command shown above. For example, if you want to get line 63 from the other viewport, do the following:


The diffget command can be shortened slightly to diffg.

It's similar for diffput.


Last modified: 03/11/2009 Tags: (none)

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top