152

Is it possible to scroll down the left and right parts of a vertically split window in Vim? I have two files I would like to compare roughly. Each line of these files looks almost the same.

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
LB40
  • 12,041
  • 17
  • 72
  • 107

6 Answers6

228

Go to the first split, and type in

:set scrollbind

Go to the next one (ctrl+w), and do the same.

To disable:

:set noscrollbind

For more info, check the documentation for scroll binding - http://vimdoc.sourceforge.net/htmldoc/scroll.html#scroll-binding

Dan
  • 10,614
  • 5
  • 24
  • 35
vyck
  • 2,281
  • 2
  • 13
  • 2
  • 69
    If you have all the files open, you can also `:windo set scrollbind`, to set it for all splits at once. – Bernhard Jan 07 '13 at 07:51
  • it's nuggets like these that make me wonder why we don't teach this stuff in school – Ralph Mar 01 '23 at 05:13
100

See the documentation for scroll-binding. You'll need to set this for each window that you want bound (e.g. a minimum of 2)

If you're comparing 2 files, however, vimdiff may be of more use

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
63
:windo set scrollbind

will set scrollbind in all windows.

Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126
  • 4
    If you find yourself doing this a lot, it might be useful to have something like `nmap :windo set scrollbind!` to toggle scrollbind in all open windows. – jlund3 Dec 17 '13 at 21:52
  • Don’t know why but `scrollbind!` did not work for me, but `invscrollbind` did. A SUPER mapping. Thanks! – UlfR Jun 27 '14 at 10:50
14

From the command line:

vim -O file1 file2 -c 'windo set scb!'

-O = open side by side.

-c = what follows in quotes is treated as a vim option.

'windo' = apply to all panels.

'scb' = shorthand for scrollbind. Saves some typing, but the two are interchangeable.

'!' = toggle. This way you can use the same command to turn it off later if you choose to.

noah
  • 2,616
  • 13
  • 27
12

G'day,

Tried using vimdiff on the two files?

vimdiff file1 file2

This will give you the scroll binding by default.

Rob Wells
  • 36,220
  • 13
  • 81
  • 146
  • 1
    the problem is that all the lines differ, it's starting to be a mess.. The lines are nearly the same but may differ by one character...and I don't want my view to be too much disturbed..thanks though. – LB40 Jul 07 '09 at 14:48
  • 1
    @LB40 you might consider tweaking your colorscheme, see http://stackoverflow.com/questions/24666558/improve-vimdiff-syntax-highlighting – qneill Jul 15 '16 at 14:20
6

For posterity, here's what I needed to do, since I didn't start with vimdiff.

I loaded one file. Then :vsp to load the other.

They are pretty different files, but I wanted to see what's common in between them.

So...

:set diff
:set diffopt=iwhite
:set scrollbind
Sam Habiel
  • 517
  • 3
  • 9
  • 6
    You can get all of those for a given buffer (:diff, :diffopt, :scrollbind) in one command with :diffthis in each of the buffers – qneill Jul 15 '16 at 14:21