How to make `Ctrl-d` and `Ctrl-u` scroll 1/3 of window height

The VimResized event fires when the window containing Vim itself is resized, i.e. your terminal emulator or GUI window. Unfortunately, there is no event that fires when Vim’s internal windows are resized.

Before I go on, you should know that the lines option also refers to the window containing Vim, and not a window within Vim. Relying on the value of lines will not give you the result that you intended to get; you want winheight() instead. Please note that this is not the same as the winheight option.

If you want to stay on the autocommand route, you would have to use some combination of Win(Enter|Leave|New), VimEnter (for the initial window), CursorHold, and InsertLeave to keep scroll in sync. This isn’t particularly efficient, however. Instead, I would suggest remapping Ctrl+D and Ctrl+U to something like the following:

nnoremap <expr> <C-d> (winheight(0) / 3) . '<C-d>'
nnoremap <expr> <C-u> (winheight(0) / 3) . '<C-u>'

This takes advantage of the fact that giving a count to Ctrl+D or Ctrl+U sets scroll and then scrolls, as mentioned in the help snippet you referenced. This should accomplish what you want without the need to rely on autocommands.

4 Likes