Vim – How to Naturally Scroll Past End of Last Line in Vim

vimvimrc

I'm a bit of a vim novice, but I'm having difficulty finding resources to help me with this problem.

When I move my cursor to the bottom of my file, I want it to keep going past the final line, preferably up until the last line is at the top of my window, but indefinitely would be fine too. My current solution consists of putting set scrolloff=20 in my ~/.vimrc and then doing G + zz to get to the bottom. But if I scroll up and back down, I'm stuck with the last line plastered to the bottom again.

I understand that because there are no newlines after the file it doesn't really have anything to scroll into, but nonetheless it's annoying that I can't edit my file with the bottom line around the middle of my window.

So ultimately I want it that when I'm at the bottom line I can hold down j and it'll just scroll down. Is there a way to do this?

Best Answer

I don't think there is a builtin option for this behaviour, but using some keybindings in your config you can keep the line with the cursor in the middle permanently (except for the top few lines of the file):

set scrolloff=99999

nnoremap <C-U> 11kzz
nnoremap <C-D> 11jzz
nnoremap j jzz
nnoremap k kzz
nnoremap # #zz
nnoremap * *zz
nnoremap n nzz
nnoremap N Nzz
nnoremap gg ggzz
nnoremap G Gzz
nnoremap gj gjzz
nnoremap gk gkzz

This maps common movements to execute zz after and center the screen on the current line. Note that this may lead to flickering on slow terminals (e.g. over ssh), as the screen position actually jumps back before centering again.

Related Question