When typing a bunch of text, the cursor gets pushed towards the bottom of the screen. I regularly have to exit INSERT mode to type zz
, so that Vim centers the line on which my cursor resides. I would like something more automatic: for instance, if the cursor is trespassing a threshold (say 5-8 lines below "the line in the center of the screen"), Vim would zz
directly, without to exit INSERT mode.
It may disrupt the typing process, but since I would be typing, my eyes would be staring at the screen and see that something is happening. I think exiting INSERT mode is more disruptive.
Is there a configuration option or a plugin supporting this use case?
You can keep the cursor's line centered by setting the 'scrolloff' option to a large value:
set scrolloff=999
Description of 'scrolloff' from the help page:
Minimal number of screen lines to keep above and below the cursor.
This will make some context visible around where you are working. If
you set it to a very large value (999) the cursor line will always be
in the middle of the window (except at the start or end of the file or
when long lines wrap).
The following will vertical center cursor automatically when its resides within last 1/3 of buffer on typing any character or entering insert mode in this region ( add to .vimrc
):
augroup autoCenter
autocmd!
autocmd InsertCharPre,InsertEnter * if (winline() * 3 >= (winheight(0) * 2))
\| norm! zz
\| endif
augroup END
:h autocmd-events
and add any other event you may need to trigger centering.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With