3

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?

1

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.

  • Yeah this works, I figured something like this was the best I was going to get. I'll leave the question open for a bit just in case more answers come in, but thanks this is great, I'll mark it as accepted if nothing else comes :) – Daniel Porteous Dec 20 '16 at 10:18
  • using O or I makes the cursor jump to the normal bottom position. is there a way to avoid this? – adantj Mar 30 '18 at 3:08
0

I found this script https://www.vim.org/scripts/script.php?script_id=1649 that does in fact work better than the previous answer. Since inserting text seems to make the scroll jump.

Mirror here: https://github.com/vim-scripts/scrollfix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.