321

I've started using vimdiff today, and wanted to do some of the things that I've taken for granted on Windows based diff editors (like expand/collapse a diff section, have full file expansion/only diffs with three context lines above or below, etc.). I currently know only the following commands:

Keyboard Shortcuts:

  • do - Get changes from other window into the current window.

  • dp - Put the changes from current window into the other window.

  • ]c - Jump to the next change.

  • [c - Jump to the previous change.

  • Ctrl+W, w - Switch to the other split window (Ctrl + W, Ctrl + W does the same thing, in case you let go of the Ctrl key a bit later)

Could someone point me to the right direction so I could replicate similar features?

It would be nice if I could expand/collapse lines around the diffs, for example.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TCSGrad
  • 11,898
  • 14
  • 49
  • 70
  • 2
    For a shortcut to add/revert a long list of changes, refer to http://stackoverflow.com/q/6093746/212942 – TCSGrad May 25 '11 at 04:25
  • See: [Use vimdiff as git mergetool](http://www.rosipov.com/blog/use-vimdiff-as-git-mergetool/) and [vimdiff – the cool way to diff for vim users](http://gingerjoos.com/blog/linux/vimdiff-the-cool-way-to-diff-for-vim-users) – kenorb May 23 '15 at 20:52
  • 1
    Its amusing to see the question being closed after more than 4 years, with it being the top 3 posts when 'vimdiff' is googled! – TCSGrad May 29 '15 at 01:19
  • See: [vimdiff cheat sheet at GitHub Gist](https://gist.github.com/4026987) – kenorb Sep 21 '16 at 16:02

4 Answers4

274

Aside from the ones you mention, I only use the following frequently when diffing:

  • :diffupdate :diffu -> recalculate the diff. It is useful when, after making several changes, Vim isn't showing minimal changes anymore. Note that it only works if the files have been modified inside vimdiff. Otherwise, use:
  • :e to reload the files if they have been modified outside of vimdiff.
  • :set noscrollbind -> temporarily disable simultaneous scrolling on both buffers, reenable by :set scrollbind and scrolling.

Most of what you asked for is folding: the Vim user manual's chapter on folding. Outside of diffs, I sometimes use:

  • zo -> open fold.
  • zc -> close fold.

But you'll probably be better served by:

  • zr -> reducing folding level.
  • zm -> one more folding level, please.

Or even:

  • zR -> Reduce completely the folding, I said!.
  • zM -> fold Most!.

The other thing you asked for, use n lines of folding, can be found at the Vim reference manual section on options, via the section on diff:

  • set diffopt=<TAB>, then update or add context:n.

You should also take a look at the user manual section on diff.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ninjalj
  • 42,493
  • 9
  • 106
  • 148
  • Very comprehensive indeed !! I'd check out the links you've said, but keeping the question open for couple more days to see if I get more replies (I posted on a weekend, and not many people would be active then). – TCSGrad Mar 14 '11 at 05:50
  • By the way, do you know if vimdiff can be used for merging/3-way resolving etc ? It would be really great then !! – TCSGrad Mar 16 '11 at 08:54
  • 1
    @shan23 For 3-way merging (for git), check [this](http://www.toofishes.net/blog/three-way-merging-git-using-vim/) out. There are comments there on svn too. Still trying to figure out the commands when you have 4 buffers though (do/dp don't work). – quornian Mar 29 '12 at 23:23
  • For `:set noscrollbind` to take effect it must also be `:set nocursorbind` used which is not the default. So both options must be adjusted. – bloody Apr 28 '20 at 17:12
  • "zm = one More folder level, please" I can never remember this one, and I love this description - thank you! – Phantomwhale Apr 16 '21 at 01:13
6

Set vimdiff to ignore case.

Having started vim diff with

 gvim -d main.sql backup.sql &

I find that annoyingly one file has MySQL keywords in lowercase, the other uppercase showing differences on practically every other line.

:set diffopt+=icase

This updates the screen dynamically and you can just as easily switch it off again.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zzapper
  • 4,743
  • 5
  • 48
  • 45
  • While this is a nice tip, I don't see how it is related to the question at hand (how to expand and collapse diff sections in vimdiff). – Paul Stelian Aug 09 '19 at 09:34
4

Actually, if you do Ctrl + W, W, you won't need to add that extra Ctrl. It does the same thing.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tubbo
  • 598
  • 10
  • 21
0

Ctrl + W, W as mentioned can be used for navigating from pane to pane.

Now you can select a particular change alone and paste it to the other pane as follows. Here I am giving an eg as if I wanted to change my piece of code from pane 1 to pane 2 and currently my cursor is in pane1.

  • Use Shift + V to highlight a line and use up or down keys to select the piece of code you require and continue from step 3 written below to paste your changes in the other pane.

  • Use visual mode and then change it

    1. Press V. This will take you to visual mode
    2. Use the up or down key to select your required code
    3. Press Esc
    4. Now use yy to copy or dd to cut the change
    5. Do Ctrl + W, W to navigate to pane2
    6. Press P to paste your change where you require it
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    The entirety of what you specified (except the first line) is a feature of vim itself, not vimdiff per say. – TCSGrad May 04 '16 at 17:04