Linux Bie
Linux Bie

Reputation: 55

Vim: Checking for blank lines below to keep the cursor position at the center

When I start with a new program, the cursor eventually gets to the bottom of the screen. I want it to remain at the center, I managed this with following in vimrc file.

autocmd InsertEnter * :set scrolloff=9999
autocmd InsertLeave * :set scrolloff=0

But when I make small changes in the code, the cursor jumps to the middle of the screen which is a bit annoying. I would like to know, how to check conditions for next blank lines(say 5), before vim executes the above commands i.e set scrolloff. I could start with 5 blank lines at the bottom while writing new code.

Upvotes: 1

Views: 278

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172520

To determine how many lines the cursor is away from the bottom of the current window, you can use

:echo winheight(0) - winline()

So, to only make the cursor jump to the middle if its in the last 5 lines, you could use:

autocmd InsertEnter * if winheight(0) - winline() < 5 | set scrolloff=9999 | endif

I don't fully understand what kind of condition you have in mind with the blank lines, but you can check individual lines with empty(getline(lnum)). Also, the built-in functions prevnonblank() / nextnonblank() might be useful.

Upvotes: 1

Related Questions