Vimdiff and Vim to Compare Files

At the moment I'm working on a tricky project where two similar projects diverged. Very similar things happened to them both, but not quite the same things - and now we're merging the codebases to give us as much common code as possible. All this simply serves to set the scene of exactly what I was doing spending a whole day with large code diffs - I had to look up a few things so I thought I'd capture them while I can remember.


I couldn't have managed this project without a great difftool and for me that means vimdiff. It's very easy to see the changes side-by-side and to push and pull them from one pane to the other - also I'm a vim user, so the navigation doesn't require any thinking from me because this is a tool I use all the time.

My vimdiff cheatsheet only contains three items:

  • ]c Go to next block of diff
  • dp Push this version of the current block into the other pane
  • do Use the block from the other pane in this pane

Today I also had to add a couple of lines to my .vimrc to get more mileage out of my diff. At some point in time, one project's coding standards had been improved or beautified or something ... but the other project didn't have those changes which were mostly whitespace and line wrapping (actually I think the other project may have had a different set of coding standards fixes applied but I like to look forward rather than dwell on how we got into any given mess).

Using diff -w gave me more or less the right output, but vimdiff was basically panicking and showing me all lines as changed. To get around this, I asked stackoverflow what to do and found this question and answers, which led me to add the following lines to my .vimrc:

set diffopt+=iwhite
set diffexpr=""

With these lines in place, vimdiff did exactly what I needed until I had changed (deleted!) so much of one of the files that it couldn't see the similarities any more. At which point, I switched to just using vim ...

Vim To Compare Files

Vimdiff only really works if the files are quite similar, I was refactoring and ended up with one file that was quite small, but still contained sections that I needed to compare to the same sections in another file. To do this, I opened both files in vertical split panes in vim, and made them scroll together. The exact steps look something like this:

  1. Open the first file in vim
  2. Type :vsplit to get two panes side by side (tip: maximise the window on your widescreen monitor before you run this command)
  3. Jump to the second pane (Ctrl+w followed by arrow key) and then open the other file :e filename
  4. Turn on scrollbind for this pane with :set scrollbind (you can turn it off later with :set noscrollbind)
  5. Move back to the first pane again (with Ctrl+w and an arrow in the direction you want to go, if you don't use splits a lot then this can be confusing) and turn on scrollbind there too
  6. To align two things (methods in my case but it could be anything), try [line number]zt to get thing you want to the top of the screen, and do the same in the other pane also. You will need to turn off scrollbinding to point to the right place, then turn it on again.

You don't have the diff features but you can very easily get the code to line up side-by-side and then skip back and forth copying/pasting/deleting/whatever as you wish. I hadn't used the scrollbind before so I found it very handy - between all these tools and a healthy set of vim macros, I got through my day!

One thought on “Vimdiff and Vim to Compare Files

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.