0

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?

Solution added after answers: No, there isn't a default way to do that. But there are these convenient mappings:

nnoremap <C-K> k<C-Y>

and

nnoremap <C-J> j<C-E>

Zaid Gharaybeh
  • 337
  • 2
  • 12
  • you can map your own, such as `nnoremap k` which will make `ctrl-e` do both motions together. – Christian Gibbons Nov 22 '19 at 22:13
  • Welcome to SO! Please see "[ask]" and the linked pages and "[mcve](https://stackoverflow.com/help/minimal-reproducible-example)". Did you research this? Where? If it didn't help tell us why. What did you try? If you didn't try, why not? If you did, what did you do? We'd like to see your minimal attempt to solve it. Currently it looks like you want us to write the script or to find it for you, either of which is off-topic. – the Tin Man Nov 23 '19 at 18:51
  • Possible duplicate of https://stackoverflow.com/a/43888638/128421. – the Tin Man Nov 23 '19 at 19:02
  • No it's not a duplicate. I do want to move both the screen AND the cursor one line up/down. That question states how to move screen WITHOUT moving cursor. – Zaid Gharaybeh Nov 23 '19 at 20:31
  • @ChristianGibbons I figured out the correct mappings. If the screen goes down one line the cursor should go up by one line and vice versa (instead of both screen and cursor going up one/down one line). This is to create the desired "scrolling" effect. The bindings I have made and tested to work are `nnoremap k` and `nnoremap j`. `` and `` are [unused bindings](https://vim.fandom.com/wiki/Unused_keys) by default in vim which is convenient since they now simulate a similar effect to `j` and `k` and allows me to keep the default functionality of `` and `` – Zaid Gharaybeh Dec 01 '19 at 22:35

2 Answers2

0

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.

Amadan
  • 169,219
  • 18
  • 195
  • 256
  • Thanks! Actually I dislike the side effects so I used slightly modified bindings than the ones Christian Gibbons suggested. The ones he suggests don't create the desired "scrolling" effect. The bindings I used are `nnoremap k` and `nnoremap j` – Zaid Gharaybeh Dec 01 '19 at 22:37
0

As there is no default way to do it, I opted to use

nnoremap <C-K> k<C-Y>

and

nnoremap <C-J> j<C-E>

Zaid Gharaybeh
  • 337
  • 2
  • 12