vimdiff – Edit two or Three versions of a file with Vim and show differences

Vimdiff starts Vim on two (or three) files. Each file gets its own window. The differences between the files are highlighted. This is a nice way to inspect changes and to move changes from one version to another version of the same file.

First you need to make sure you have installed vim editor

#apt-get install vim-full

This will complete the installation

Using vimdiff

vimdiff syntax

vimdiff [options] file1 file2 [file3]

gvimdiff

You may also use “gvimdiff” or “vim -g”.  The GUI is started then.You may also use “viewdiff” or “gviewdiff”.  Vim starts in readonly mode then.”r” may be prepended for restricted mode

vimdiff examples

If you want to open files use the following command

vimdiff file1 file2

or

vim -d file1 file2

If you want to open vertical splits for your files use the following command

vimdiff -O file1 file2

If you want to open horizontal splits for your files use the following command

vimdiff -o file1 file2

If you want to open files in perticular directory use the following command.Vim first append the file name of the first argument to the directory name to find the file.

vimdiff file1 file2 [file3 [file4]]

or

vim -d file1 file2 [file3 [file4]]

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.

:diffupdate – diff update

:syntax off – syntax off

zo – open folded text

zc – close folded text

You can also use tkdiff,meld and xfdiff4 as an alternative

Sponsored Link

3 thoughts on “vimdiff – Edit two or Three versions of a file with Vim and show differences

  1. Thanks. Will add this to my toolbox. Another quality alternative is “diffuse”

  2. Hi,

    Thanks for the article. I’m also a fan of vimdiff, and I use it to help me merging git branches. Just add the lines


    [merge]
    tool = vimdiff

    Then the command “git mergetool” will pick it up automatically. It’s indeed a productivity booster!

    Also I’d recommend the peaksea color scheme for VIM to be used with vimdiff. With peaksea you don’t have to worry about the diff color markup conflicting with the syntax highlighting itself — when is the last time you opened two documents in diff mode only to find text “gone” because the syntax highlight color happens to be the same as diff background color? With peaksea I never had a problem.

    You can also configure VIM so that peaksea is only activated in diff mode. To do this, put someting like this in your ~/.vimrc:

    if &diff
    set t_Co=256
    set backgroud=dark
    colorscheme peaksea
    else
    colorscheme YOUR_OTHER_COLOR_SCHEME_OF_CHOICE
    endif

    Enjoy! 😉

Leave a comment

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