How to move screen without moving cursor in Vim?

2019-01-20 21:05发布

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?

10条回答
你好瞎i
2楼-- · 2019-01-20 21:11

There is a new plugin which I wrote, it enables you to navigate the hole file without moving the cursor position. It's based on folding the lines between your position and your target position and then jump over the fold. Or abort it and don't move at all.

It's also easy to fast switch between cursor is the firt line, cursor is the last line and cursor is in the middle by just clicking j, k or l (when you are in the mode of the plugin.)

I guess it would be a good fit here: https://github.com/anschnapp/move-less

查看更多
再贱就再见
3楼-- · 2019-01-20 21:20

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.

查看更多
smile是对你的礼貌
4楼-- · 2019-01-20 21:24

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

查看更多
Luminary・发光体
5楼-- · 2019-01-20 21:26

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.

查看更多
再贱就再见
6楼-- · 2019-01-20 21:28

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
查看更多
家丑人穷心不美
7楼-- · 2019-01-20 21:30

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.

查看更多
登录 后发表回答