21 Jun

columnar coding in vim

I like to see as much code on the screen as possible. I also like to use short lines of code, as they are easier to read.

Combining the two, though, usually means that I end up with a screen with code on teh left side of the terminal, and nothing on the right – not ideal.

So, I posed a question on the vim mailing list, and got an answer about two hours later from Chip.

The solution is to split the screen into two columns, scroll the right column down one page, and “lock” the scrolls together, so that scrolling one column by a line causes the other to also scroll.

To do this, first place this function in your ~/.vimrc:

" Scb: toggle all windows to/from scrollbind {{{2
com! -nargs=0 Scb call ScrollBindToggle()
fun! s:ScrollBindToggle()
  if &scb == 0

   let wn                 = winnr()
   let g:scrollbindtoggle = &sbo
   set sbo=ver,hor,jump
   windo set scb
   exe wn."wincmd w"
  else
   let wn   = winnr()
   let &sbo = g:scrollbindtoggle
   unlet g:scrollbindtoggle
   windo set noscb
   exe wn."wincmd w"
  endif
endfun

Then, load up a file of code which usually takes up two or more screens to view it.

Now, split the page in two columns, select the right one, scroll that down a page, lock the scroll, and go back to the first column.

:vsp
<CTRL+w> →
<PGDN>
:Scb
<CTRL+w> ←

That’s it!

4 thoughts on “columnar coding in vim

  1. This is exactly what I’ve been looking for, but after typing :Scb I get the following error:

    E117: Unknown function: ScrollBindToggle

    I see that the screenshots are from Windows, and I’m using Linux. Maybe this is the problem?

  2. the screenshot is of a terminal logged in through ssh to my home linux server.
    maybe it’s your version of vi? I’m using 6.3 on all my machines, and have no problems.

  3. oh! I just tried to copy and paste from the code above, and the quote marks are wrong. I’ll fix my wordpress so it doesn’t do any magical quote updating on it. Try copying the code again.

  4. Truly, the quote marks around “wincmd w” at two places got copied into reverse questionmarks. I changed them to “, but it didn’t solve the problem. Since Vim is complaining that the function is unknown, I thought that the problem must be in the header. I don’t know this Vim scripting language, but as I didn’t know what is this “s:” in the “fun! s:ScrollBindToggle()” line for, I removed it. So I changed the line to “fun! ScrollBindToggle()”, and now it works. No idea what I did. I’m using the 6.3.4 version.

Comments are closed.