For a vim user, vimdiff is a good tool for:-

  • Code reviews
  • Merging of two versions

This post focusses on two parts:

  • Useful commands in vimdiff
  • Recursive diff of directories.

vimdiff

Following ways can be used to invoke:-

vimdiff file1 file2

vim -d file1 file2

This opens up the files in vertical split mode.

To open in horizontal split mode, one should use -o option

vimdiff -o file1 file2

Default settings are generally good for viewing and understanding the diff.

vimdiff1

As you can see in the screenshot, vimdiff highlights the differences in different background colors.

(Colors can be different based on your color scheme)

- Blue is there for new block

- Purple is there for a differing line

- Red is there for differing characters in the differing line

The above colors can be different in your terminal depending on colorscheme in your setup.

CtrlW + CtrlW (Twice ^w) – Command can be used to go to other window (Same as multiple files)

Ideally, you can do a yank (yy) and pate(p) for different sections and do the merging.

Following commands are useful for effective merging

dp – ‘put’ the difference under to the other window

do – ‘obtain’ the difference under from other window.

]c – go to the next difference

[c - go to the previous difference

Specify count with above to jump some changes

zo – ‘open’ a section of folded text.

zc – ‘close’ a foldable text section.

:diffupdate – updates diff based on changes (Note: du is NOT a shortcut)

Directory diff

DirDiff.vim is a useful plugin for recursively diff and merge files.

Create ~/.vim/plugin folder if not already present and copy DirDiff.vim to this folder

Available at:-

http://www.vim.org/scripts/script.php?script_id=102

From any file opened with vim and give this command

: DirDiff dir1 dir2

You get a vimdiff on top and the files selection below. Any vimdiff commands mentioned can be used.

DirDiff1

From the below panel, you can select a file and then issue a command ‘s’

Various parameters for copying current diff or thw whole file come up as shown in picture.

As you can see, if you press ‘l’, you can copy whole of left side file to right side

Hope this saves some time when use vimdiff next time.

Tagged with →  
Share →

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>