Dreams of Thought

Are dreams thoughts… or are thoughts dreams..

RSS Feed

vimdiff – the cool way to diff for vim users

7 Comments
Posted by Anirudh on October 20, 2009 at 9:30 PM

Some of you using the vim editor may not know about a tool that comes with vim called vimdiff. Vimdiff is an awesome way to diff files if you are a vim nut. It gives you the power of vim + the power of diff.

How’s this different from opening 2 files in vim (with -O option) you ask ? The difference is that vim will highlight the diff for you.

Fire it up by giving the 2 filenames, say

$vimdiff old new

This will bring up a screen like this -

vimdiff

vimdiff

Now you can move around the 2 parts of the screen with your regular vim commands. For eg. use (Ctrl+w) + right arrow to move to the right half of the screen.

vimdiff with cursor on right half of screen

vimdiff with cursor on right half of screen

You can copy paste as well. Go to the 2nd line in the left half and press the y key twice to copy that line. Use (cntrl + w) + right arrow to move cursor to the 1st line in the right half of the screen. Press p to paste the copied line below the 1st line.

vimdiff with copy paste

vimdiff with copy paste

You can go to the first line and delete the two words “a new” by moving the cursor to “a” and hitting d2w key combo.

vimdiff deletes words

vimdiff deletes words

Insert “an old” there by going to insert mode(press i key) and then typing the two words. You’ll see that vimdiff does not highlight anything. This means that there is no difference between the two files.

two files with no difference in vimdiff

two files with no difference in vimdiff

Vimdiff is the same as bringing up vim with the -d option. You could do the same things you did above by using

$vim -d old new

Bonus :
You can also diff 2 URLs directly with vimdiff
Try
$vimdiff 'http://www.google.co.in/search?q=vimdiff' 'http://www.google.co.in/search?q=vim'
You’ll first see something like this coming on screen

:!curl -o '/tmp/v959288/1' 'http://www.google.co.in/search\?q=vimdiff'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
"/tmp/v959288/1" 4L, 5289C
:!curl -o '/tmp/v959288/2' 'http://www.google.co.in/search\?q=vim'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
"/tmp/v959288/2" 4L, 5253C
Press ENTER or type command to continue

Now press the ENTER key to see the diff

vimdiff also works directly with URLs

vimdiff also works directly with URLs

You can throw in ssh into the mix as well. Try something like this
$vimdiff old <(ssh user@host cat ~/new)

If you're a vim/vimdiff ninja and know some more tricks, do post them below :)

Happy hacking...

Filed under Linux
Tagged as , , , ,
You can leave a comment, or trackback from your own site.

7 Comments

  • On November 18, 2009 at 6:08 PM John Smith said

    If you have differences between files, you can use the keys ‘do’ and ‘dp’ to automatically copy from the second file and to the second file, respectively.

  • On November 23, 2009 at 12:23 PM Anirudh said

    nice to know, John. Thanks :)

  • On May 12, 2010 at 11:43 AM rob said

    This is awesome. You have just made my day. I can’t wait to use it at work tomorrow.

  • On November 23, 2010 at 11:04 AM Anirudh said

    Thanks.. that was great :)

  • On November 23, 2010 at 4:25 PM Alvaro Zuniga said

    The following command will get all files that have text banana, peanut or bread in the current directory and any other directory under it. The files are opened in multiple buffers.

    vim `grep -REi ‘(banana|peanut|bread)’ . | cut -f 1 -d : | sort -u`

    Remove the R to only search the current directory and the i for case sensitivity.

    The following is a simpler form that will load any php file with the world include. Notice the lack of E for extended regular expressions

    vim `grep ‘include’ *.php | cut -f 1 -d : | sort -u`

    Once the files are opened the following commands can be used

    :ls
    shows the files currently open

    :bN
    jumps to the appropriate file where N is a number of a file

    :bn and :bp for buffer next and previous etc.

  • On June 7, 2013 at 9:37 PM Stuart MacDonald said

    Instead of
    vim `grep -REi ‘(banana|peanut|bread)’ . | cut -f 1 -d : | sort -u`

    use
    vim `grep -REil ‘(banana|peanut|bread)’ .`

  • On June 20, 2013 at 6:07 PM Thiru said

    How to copy(copy from file1 to file2) the lines when we do vimdiff.

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>