How to move screen without moving cursor in Vim?

0 votes
asked Aug 11, 2010 by ereon

I recently discovered Ctrl+E and Ctrl+Y shortcuts for Vim that respectively move the screen up and down with a one line step, without moving the cursor.

Do you know any command that leaves the cursor where it is but moves the screen so that the line which has the cursor becomes the first line? (having a command for the last line would be a nice bonus).

I can achieve this by manually pressing Ctrl+E (or Ctrl+Y) the proper number of times, but having a command that somehow does this directly would be nice.

Any ideas?

9 Answers

0 votes
answered Aug 11, 2010 by gww

Vim requires the cursor to be in the current screen at all times, however, you could bookmark the current position scroll around and then return to where you were.

mg  # This book marks the current position as g (this can be any letter)
<scroll around>
`g  # return to g
0 votes
answered Aug 11, 2010 by zyx

You may find aswers to this question useful: Scrolling Vim relative to cursor, custom mapping: you can use ScrollToPercent(0) from that question to do this.

0 votes
answered Aug 11, 2010 by kevin-vaughan
  • zz - move current line to the middle of the screen
  • zt - move current line to the top of the screen
  • zb - move current line to the bottom of the screen
0 votes
answered Aug 27, 2012 by paul-wheeler

Additionally:

  • Ctrl-y Moves screen up one line
  • Ctrl-e Moves screen down one line
  • Ctrl-u Moves cursor & screen up ½ page
  • Ctrl-d Moves cursor & screen down ½ page
  • Ctrl-b Moves screen up one page, cursor to last line
  • Ctrl-f Moves screen down one page, cursor to first line

Ctrl-y and Ctrl-e only change the cursor position if it would be moved off screen.

Courtesy of http://www.lagmonster.org/docs/vi2.html

0 votes
answered Aug 6, 2015 by oleg-khalidov

Sometimes it is useful to scroll text with K and J keys. So I have this "scroll mode" function in my .vimrc (also binded on zs)

scroll_mode.vim

0 votes
answered Aug 18, 2016 by alexander-rumyantsev

To leave the cursor in the same column when you use Ctrl+D, Ctrl+F, Ctrl+B, Ctrl+U, G, H, M, L, gg

you should define the following option:

:set nostartofline
0 votes
answered Sep 15, 2017 by nicolas-raoul

zEnter does exactly what this question asks for.

It works where strangely zz would not work (vim 7.4.1689 on Ubuntu 2016.04 LTS with no special .vimrc)

0 votes
answered Sep 15, 2017 by herrserker

You can prefix your cursor move commands with a number and that will repeat that command that many times

10Ctrl+E will do Ctrl+E 10 times instead of one.

0 votes
answered Sep 15, 2017 by henry

Here's my solution in vimrc:

nnoremap k kzz
nnoremap j jzz
nnoremap p pzz
nnoremap P Pzz
nnoremap G Gzz
nnoremap x xzz
inoremap <ESC> <ESC>zz
nnoremap <ENTER> <ENTER>zz
inoremap <ENTER> <ENTER><ESC>zza
nnoremap o o<ESC>zza
nnoremap O O<ESC>zza
nnoremap a a<ESC>zza

So that the cursor will stay in the middle of the screen, and the screen will moves up or down.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...