VIM as a Diff/Merge Tool 17

Posted by tottinger Thu, 01 May 2008 02:24:00 GMT

Do you need a really quick, clean, powerful diff/merge tool that you can use in windows, mac, and various Unixen? As a happy and pround vim user, I have been enjoying vimdiff for a number of years.

        > gvim -d file1 file2
        > vimdiff file1 file2

(G)vimdiff is wonderful. You can move changes between two versions of a file with great ease (if you know vim) and amazing speed. It can handle files of any size and does a fine job of syncing up both versions.

Vim can do just about anything. I have learned that no matter how much I use and study Vim I still am just scratching the surface of what the editor can do. I find little tricks and configuration options, new keystrokes, and fun little bits of minutae. I think it’s all new. With my memory, sometimes I just think something is new, and have to go check my past notes to see if I already knew it.

Today I (re)learned about diffopt, which makes gvimdiff more wonderful.
    :set diffopt=filler
By default, diffopt is set to “filler”. Filler doesn’t do much, it just adds vertical spaces to keep the text of the left and right pane aligned. It’s a good setting, and I don’t much like working without it. But there are other settings which are overlooked, even by experts.
    : set diffopt+=iwhite
My second favorite is ‘iwhite’, which stands for “ignore whitespace”. It makes gvimdiff ignore leading, trailing, and embedded spaces so that simple acts like retabbing or deleting vertical spaces won’t obscure actual changes. It really helps you to merge changes in situations where you’ve adopted code that was built to entirely different standards.
    :set diffopt+=icase
iCase ignores capitalization, which is not as useful as the other options, since programmers are normally interested in case, but it may be handy when someone has corrected and reformatted SQL. I don’t expect to use it much.
    :set diffopt+=horizontal
The last interesting bit is the choice between vertical splits (the normal case) and horizontal splits (for very long lines on cinema-aspect screens). I hope to never need this option again, but I have needed to deal with absurdly long lines.

If you can use VIM at all, you will find vim’s diffmode to be a very powerful and useful tool. If you can’t use vim already, then maybe vimdiff will be the reason you will learn vim.

Comments

Leave a response

  1. Avatar
    Pádraig Brady about 12 hours later:

    I’ve not liked vimdiff when I tried it. I’ll try again I promise. My current merge tool of choice is meld, which I’ve described here: http://www.pixelbeat.org/programming/diffs/

    The important thing to note anyway is that using these tools, is infinitely better than merging manually.

  2. Avatar
    nicholas a. evans about 13 hours later:

    I’m an avid vim user, and I often use vimdiff when merging simple changes. But when I’m dealing with conflict resolution, I usually resort to kdiff3, which is available under Linux, Windows, and Mac OS X.

    Although I think that meld looks nicer than kdiff3, the UI and workflow of kdiff3 seem the most intuitive to me when dealing with three-way diffs for conflict resolution. And vimdiff is faster and closer to hand than meld when dealing with smaller diff tasks.

  3. Avatar
    A Purohit about 14 hours later:

    I use vim and its diff tool very heavily. I have defined the following shortcuts

    nnoremap <Leader>u :diffupdate<cr>

    nnoremap <Leader>g :diffget<cr>

    nnoremap <Leader>p :diffput<cr>

    (my leader is the comma character)

    so I can type ,g to get ,p to put diffs (both commands work with respect to the current window) and ,u to refresh the screen. It really speeds things up.

  4. Avatar
    Chris Gaal about 14 hours later:

    I’m an avid Vim user as well, and vimdiff has become an indispensable part of my daily tool kit.

    I’d offer two other tips on the topic of using Vim as a diff tool:

    - Check out the DirDiff plugin (google “vim dirdiff”). It’s very nice for viewing diffs of directory contents.

    - I’ve found it helpful to map Vim’s commonly used diff commands to single keys, and adding in a “zz” command to center the window on the current difference when jumping to the next diff. This allows to you fly though diffs, quickly obtaining/putting changes. I use the following, which uses similar key bindings to Araxis Merge (a tool I used in my pre-Vim life):

    nmap <F7> [czz

    nmap <F8> ]czz

    nmap <F2> do

    nmap <F3> dp

  5. Avatar
    kretik about 15 hours later:

    Unless you’re doing this because you’re running on *nix, there’s no reason not to use WinMerge. It’s light years ahead of vim’s diff functionality.

  6. Avatar
    David Paxson about 16 hours later:

    I used VIM’s diff feature to compare/merge a base file along with two branches from it. I also had a three monitor setup with the diff spread across the three monitors.

  7. Avatar
    Rob van Eijk 1 day later:

    I discovered merging through gVim. It works like a charm. I use it to keep my dokuwiki main.php up to date when doing updates.

  8. Avatar
    Tim 3 days later:

    If you are on *nix, don’t overlook meld. It’s quite a nice tool also. I just tend to do more work with VIM than not.

  9. Avatar
    AqD 3 days later:

    I found it hard to use for large files without the graphical indication of where the differences are…. There are a lot of GUI diffs that are better for the job and I’d use vimdiff only when I’m on console mode.

  10. Avatar
    pb 4 days later:

    Just what I want – to type in a stupid command line instead of selecting two file and right clicking to choose diff.

  11. Avatar
    Lorne 4 days later:

    You can select two files, right click, and select ‘Diff with Vim’. Easy enough???

  12. Avatar
    tim 4 days later:

    Huh? No graphical indication? You must not have been in diff mode, or you didn’t have syntax enabled. The differences are pretty stark if you have color syntax highlighting turned on, and you have shortcut keys to move between differences.

    When I use vimdiff, it collapses the areas where there are no differences. I see a few lines of context, and clearly colored areas of difference (including space changes within a line), and am able to copy changes between the panes pretty easily.

    Maybe there needs to be some tutorial here. With screenshots.

  13. Avatar
    Jacob Nikom about 1 month later:

    I have question about vimdiff – how to do the actual merging? I mean the change in my files using vimdiff. I can see the difference between the two files very clearly in vimdiff, but I would prefer to be able line by lile to merge those differences. Is there such vimdiff command which applies the difference to specific line of the code.

    Thank you,

    Jacob Nikom

    jacob_nikom@ieee.org

  14. Avatar
    Tim about 1 month later:

    @Jacob Nikom

    Just edit using vim normally. You can cut and paste between the two, and the vimdiff adjusts quite naturally. Remember the ^WL and ^WH to jump between windows.

    Just play with it a little bit and you’ll see how it works.

  15. Avatar
    Vimmer 2 months later:

    You can also use :diffput and :diffget to do the merging. It’s much better than manual copying and pasting.

    The commands have shortcuts in normal mode (‘dp’ and ‘do’ respectively).

    Those who are not experienced Vim users will understandably not appreciate vimdiff. But for us Vim users, vimdiff is light years ahead of any another diff/merge tool. And yes, I did try most of the others that were suggested in the thread. vimdiff is better because it enables me to use all other Vim features.

  16. Avatar
    tca 12 months later:

    I have to say, I use Gvim for 98% of my coding and have done so for years. But when it comes to diff, I much prefer Meld over gvimdiff. Meld wins me over with its scm support allowing me to easily browse my mercurial directory and I still have it open gvim when I edit. Even without that, however, the diffs are simply a bit cleaner in Meld imho.

  17. Avatar
    Evgeny about 1 year later:

    These tips are nice, but it would really be great if vimdiff supported 3-way merging like most other tools do it.

    Show 3 files at the top, and show the result at the bottom – and some quick way to choose (in the result) which version should be inserted (yours,base,theirs).

    Also there must be a way to jump to the next diff … I just need to find they keys for it in vim …

Comments