Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll (move screen up/down while cursor remains fixed at same screen position) in Vim

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?

like image 436
WalksB Avatar asked Sep 14 '25 05:09

WalksB


1 Answers

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.

like image 77
Amadan Avatar answered Sep 16 '25 03:09

Amadan