Using VimDiff for file comparison and merge

How to use:

1. From terminal –

$vimdiff file1 file2

2. From Vim –
Open vim and use the following commands if you already have windows with other buffers open in the current tab.

:tabe file1
:vert diffsplit file2


:e file1
:vert diffsplit file2

– If you have two or three windows open in your current tab and they display the buffers that you want to diff, you might want to turn on diff mode for each window:

:windo diffthis

or, synonym:

windo set diff

– vimdiff will correctly highlight differences inside a line (whole line pink, differing characters red).

– If you load up two files in splits (:vs or :sp), you can do :diffthis on each window and achieve a diff of files that were already loaded in buffers

– turning off:
:diffoff can be used to turn off the diff mode.
issuing :diffoff! which will turn off diff mode for all windows in the current tab page

Keyboard Shortcuts:
do – Get changes from other window into the current window.
dp – Put the changes from current window into the other window.
]c – Jump to the next change.
[c – Jump to the previous change.
Ctrl W + Ctrl W – Switch to the other split window.

Tips on file comparison:

– Sometimes the difference can be a special character – :set list in Vim will show whitespace. End of lines show as ‘$’ and carriage returns usually show as ‘^M’.

– Counting number of characters in a file from bash –

wc -c filename

Useful resources:
1. vimdiff – the cool way to diff for vim users
2. Vim documentation: diff
3. Diffuse -graphical tool for merging and comparing text files


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s