Vimdiff and Vim to Compare Files
Join the DZone community and get the full member experience.Join For Free
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:
]cGo to next block of diff
dpPush this version of the current block into the other pane
doUse 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).
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
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:
- Open the first file in vim
:vsplitto get two panes side by side (tip: maximise the window on your widescreen monitor before you run this command)
- Jump to the second pane (
Ctrl+wfollowed by arrow key) and then open the other file
- Turn on scrollbind for this pane with
:set scrollbind(you can turn it off later with
- Move back to the first pane again (with
Ctrl+wand 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
- To align two things (methods in my case but it could be anything), try
[line number]ztto 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!
Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.