stephenmm
stephenmm

Reputation: 2670

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

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

Upvotes: 8

Views: 367

Answers (2)

Brian Carper
Brian Carper

Reputation: 72976

You could do this:

nmap <C-Down> <C-E><Down>
nmap <C-Up> <C-Y><Up>

Upvotes: 0

thedz
thedz

Reputation: 5572

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.

Upvotes: 5

Related Questions