308

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 3 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.

L Y E S - C H I O U K H
  • 3,827
  • 7
  • 32
  • 52
TCSGrad
  • 11,324
  • 13
  • 46
  • 68
  • 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

253

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

  • :diffupdate :diffu -> recalculate the diff, useful when after making several changes vim's 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: vim user manual's chapter on folding. Outside of diffs I sometime 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.

laurent
  • 79,308
  • 64
  • 256
  • 389
ninjalj
  • 39,486
  • 8
  • 94
  • 141
  • 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
4

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

Louis Barranqueiro
  • 8,594
  • 5
  • 35
  • 49
tubbo
  • 579
  • 8
  • 20
4

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 & you can just as easily switch it off again

zzapper
  • 4,003
  • 5
  • 44
  • 41
  • 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
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 click 'v' this will take you to visual mode 2 use up or down key to select your required code 3 click on ,Esc' escape key 4 Now use 'yy' to copy or 'dd' to cut the change 5 do 'ctrl + w, w' to navigate to pane2 6 click 'p' to paste your change where you require

  • 1
    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