When scrolling normally on a webpage, the cursor remains at the same place on the screen while the screen shifts smoothly up/down. I would like to replicate that functionality in vim. Ctrl + Y and Ctrl + E move the screen but the cursor does not stay at the same position on screen, it stays on same row.
So far, the only thing that comes close is Ctrl + U and Ctrl + D, but this scrolls half the screen's worth in rows. This doesn't enable smooth scrolling.
Is there a built-in command for that?
The 'scroll'
option determines the number of lines Ctrl-U and Ctrl-D scroll. The default of 0
is "half a screen"; but if you
:set scr=1
then those commands will only move you by one line. (You can get back to the default using :set scr=0
.)
Note that 'scroll'
option is automatically set by prefixing Ctrl-U or Ctrl-D with a count. I.e. if you do 1Ctrl-D, every further Ctrl-U or Ctrl-D will only move by one line until 'scroll'
is reset.
Another possibility is 'scrolloff'
, which determines the minimum number of lines above and below the cursor in a window. Setting it to a ridiculous number will make sure your cursor is always in the centre of the screen. That is,
:set so=999
will de facto convert j
and k
into what you desired.
Finally, a binding like Christian Gibbons suggests in comments is another way to do this, if you dislike the side effects of these two methods.
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