8

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?

4 Answers4

2

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.

crater2150
  • 3,946
  • 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 03:08
2

You could also move the current cursor line to particular place on the screen.

I use <Ctrl-E> to shift the current line up by one line.

You could also do zt to move the current line to the top of the page.

More info here

AdminBee
  • 22,803
g.delgado
  • 121
1

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

adantj
  • 113
  • 5
  • 1
    Thanks, this small plugin fixed my issue, since it looks like the INSERT mode has quirks with Gzz when scrolloff=999 is set. Plus, it can set the cursor at a fixed position other than the middle (e.g. scrollfix=45). – Ranel Padon Jul 13 '22 at 13:23
0

You can use a conditional remap on j. If you are on the last line of the document then map j to <C-e>. If you are on any other line map j to j.

nnoremap <expr> j line(".") == line('$') ? '<C-e>':'j'

line(".") returns the current line number and line("$") returns the last line number.

Cardano
  • 103