Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to: assign vim cursor to a certain line in the window and have the text scroll underneath that cursor position?

Tags:

vim

scripting

You can keep the cursor line in the middle of the screen and then have the text scroll underneath it by setting the scrolloff to a very large number. Ex:

:let &scrolloff = 999

To read more about how this works:

:help scrolloff

I love this feature and use it all the time but I would like the ability to keep the cursor at other locations other than the middle of the screen. For instance I would like the ability to keep the cursor at the top of the window and have the file scroll underneath it. I am pretty sure there is nothing natively available in vim to do this so I was wondering if anyone had come up with a light wieght vim script snippet to do this (or can someone come up with such a script)?


Here is my little .vimrc helper code to swich the position of the cursor quickly:

" SCROLLFIX SHORTCUTS
function! ToggleMyScrollFix()
  if ( g:scrollfix == 5 )
    let g:scrollfix = 50
  elseif ( g:scrollfix == 50 )
    let g:scrollfix = 95
  elseif ( g:scrollfix == 95 )
    let g:scrollfix = 5
  else
    let g:scrollfix = 50
  endif
endfunction
nnoremap  <silent> zz :call ToggleMyScrollFix()<CR>lh
like image 930
stephenmm Avatar asked Jul 24 '09 19:07

stephenmm


People also ask

Can you scroll in Vim?

You can make Vim scroll the text using the shifted up/down arrows by mapping Shift-Up to Ctrl-Y and Shift-Down to Ctrl-E. Shift-Down will then scroll down (like moving a scroll-bar down, or like moving a cursor at the bottom of a window down), and Shift-Up will then scroll up (like moving a scroll-bar up, etc).

How do I go down in Vim?

Press the ESC key to change normal mode. Press the CTRL+f key in order to page down or move forward.


1 Answers

Check out the scrollfix plugin. I used it a couple years ago, so I don't know if it'll still work with the latest snapshots of Vim -- but at worst, it should give you a pretty good start at modifying it to work for you.

like image 107
thedz Avatar answered Sep 21 '22 19:09

thedz