Code merging with VIM

So today I had the chance to learn about vimdiff, VIM’s mode for file comparison and merging. It was a lot better than I expected!

When I switched to using Ubuntu for my day-to-day local testing (because I need reliable Xdebug, and Windows Vista’s Apache-PHP module just wasn’t cutting it as far as Xdebug stability goes), I was worried I wouldn’t have a good merging tool to go along with it. VIM’s been working great as an IDE (just when I was about to go back to my slower XP image, too!), but I was wondering about this vimdiff thing I’d heard about. I figured today that, since a couple conflicts came up in my Bazaar merge, I’d give vimdiff a whirl.

I ran it like this:

vimdiff file1 file2

and VIM appeared, nicely split into two panes and with all the changes highlighted – just like WinMerge! This was cool so far.

Then I had to figure out how to actually, you know, merge the changes. So I typed :help vimdiff and got the help file.

I scrolled through, and picked up the only 3-4 commands I will probably ever need. And here they are for you. Note that these assume you’re just diffing two files…which is all I’ve ever done, so you probably are…and if not, you’re probably smarter than me anyway. And they’re all used in VIM’s normal mode (the one it starts in, unless you’re a Creamer) unless otherwise mentioned.

VIMDIFF COMMAND #1 – JUMP TO NEXT CHANGE

]c – Jump to the next difference. Equivalent to alt + down in WinMerge.

VIMDIFF COMMAND #2 – JUMP TO PREVIOUS CHANGE

[c – Jump to the previous difference. Equivalent to alt + up in WinMerge.

VIMDIFF COMMAND #3 – MERGE LEFT

do – Copy the current difference’s text from the file on the right to the file on the left. Equivalent to alt + left in WinMerge. (The VIM help said it’s do and not dg because dg is too close to dgg – a smart move on their part, as I enjoy not deleting to the beginning of my buffer. But if you ever wanna do that, hey, now you know how.)

VIMDIFF COMMAND #4 – MERGE RIGHT

dp – Copy the current difference’s text from the file on the left to the file on the right. Equivalent to alt + right in WinMerge.

BONUS VIMDIFF COMMAND – UPDATE THE DIFF DISPLAY

Occasionally, as you’re happily merging away, your vimdiff display may get out of whack for some reason. VIM does a pretty good job of preventing it (hasn’t happened to me yet), but if you want to make sure you’re all good, just type :diffupdate. Then continue merging. (If you’ve been reading the WinMerge shortcut mapping, this one’s equivalent to good ol’ F5.)

Bazaar’s bizarre merge markers finally kinda made sense after merging with vimdiff!

Thanks VIM people. Maybe I will be donating to Uganda soon :)

I hope this article was useful. If you’ve got any extra tips, post them in the comments!

And hey, if you’re merging on Windows, WinMerge stands as an awesome tool: http://winmerge.org.

Talk to you soon, and happy merging.

This entry was posted in Life of a Web Developer. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. just ujo
    Posted February 17, 2011 at 12:59 am | Permalink

    This is probably the best tutorial which I found on the internet about merging using vim. Thanks a lot and keep up the good work. Cheers

Post a Comment

Your email is never published nor shared. 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>