Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to automatically make Vim vertically center the line when typing?

Tags:

vim

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?

like image 226
raphaelfournier Avatar asked Sep 18 '25 11:09

raphaelfournier


2 Answers

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).
like image 182
Nefrubyr Avatar answered Sep 23 '25 05:09

Nefrubyr


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.

like image 33
dNitro Avatar answered Sep 23 '25 05:09

dNitro