Skip to content

13.2. vimdiff two files

shinokada edited this page Oct 6, 2014 · 1 revision
vimdiff file1 file2
vim -d file1 file2

In vim:

:vert diffsplit file2

To open the diff in a new tab in the existing window:

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

Scrolling

To get both files scrolling at the same time, add this to .vimrc

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:

:diffupdate

Moving around

Switch viewport:

ctrl-w w

Jump to next diff:

]c

Jump to previous diff:

[c

Merging

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:

do

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:

dp

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:

:63diffget

By 'ctrl+w' to change the viewport and use :5diffget

The diffget command can be shortened slightly to diffg.

It's similar for diffput.

Resource

Diff and merge using vim

Clone this wiki locally