How can I expand/collapse a diff sections in Vimdiff?

LinuxVimDiffCommand Line-InterfaceVimdiff

Linux Problem Overview


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.

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

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.

Solution 3 - Linux

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

Solution 4 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTCSGradView Question on Stackoverflow
Solution 1 - LinuxninjaljView Answer on Stackoverflow
Solution 2 - LinuxzzapperView Answer on Stackoverflow
Solution 3 - LinuxtubboView Answer on Stackoverflow
Solution 4 - LinuxPratheusha KKView Answer on Stackoverflow