Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim scrolling without changing cursors on-screen position

When cursor is at middle of screen and i scroll down, the cursor moves upwards on the screen. I don't want it to do that.

How can i scroll without changing cursors on-screen position?

Solution, added after answer:

noremap <C-k> 14j14<C-e> noremap <C-l> 14k14<C-y> 
like image 886
john-jones Avatar asked Aug 17 '10 14:08

john-jones


People also ask

How do I enable scrolling in Vim?

":set mouse=a" is used to enable mouse scrolling/selection.

How do I scroll in Vim?

You can make Vim scroll the text using the shifted up/down arrows by mapping Shift-Up to Ctrl-Y and Shift-Down to Ctrl-E. Shift-Down will then scroll down (like moving a scroll-bar down, or like moving a cursor at the bottom of a window down), and Shift-Up will then scroll up (like moving a scroll-bar up, etc).

How do I scroll in vi editor?

To scroll forward (move down) one screenful, press Ctrl-F. (Hold down the Control key and press the F key.) The cursor moves to the upper left corner of the new screen.


2 Answers

There are two ways I can think of: ctrl-E and ctrl-Y scroll the buffer without moving the cursor's position relative to the window. I think that is what you want. Also, if you set scrolloff to a large number, you will get the same effect as ctrl-E and ctrl-Y with the movement keys. scrolloff setting will make it hard to get the cursor to move vertically relative to the window though. (Use something like :set so=999, so is an abbreviation for scrolloff.)

:help 'scrolloff' :help scrolling 
like image 133
Alok Singhal Avatar answered Oct 12 '22 04:10

Alok Singhal


ctrl-D and ctrl-U is what you want.

ctrl-D has the same effect as 14j14<C-e> (just that the number 14 is not hard coded and the amount of movement depends on the actual size of your screen): You move the cursor several lines down in the text but the cursor stays in the middle of the screen.

Similarly ctrl-U works like 14k14<C-y>.

Addendum: If your screen has 30 lines then the the two are exactly the same.

like image 44
Matthias 009 Avatar answered Oct 12 '22 03:10

Matthias 009