在 vi/vim 中从当前位置移动光标 x 行

有没有办法在 vi/vim 中移动光标相对大量的行?假设光标位于要删除的代码块下10行。如果您有以相对顺序显示的行号,那么最好有一个“向上跳转10行命令”可以将您带到那里。

或者最好是显示绝对行号,然后去 xgg,其中 x 是行号?

58143 次浏览

Yep, of course there's a way. j and k move down and up one line, so 10j and 10k move down and up ten lines. You can repeat any motion by putting a number before it.

You might also want to set relativenumber if this is something you do a lot of - it'll help save you counting by printing line numbers relative to the current line, instead of absolute numbers.

Moving 10 lines up and down might not suit your task as well as other options. Consider other movements:

Ctrlf, Ctrlb page forward and back.

}, { move forward and back by one paragraph.

You can write rules in your vimrc to bind 10j to a key, say J to move down 10 lines by adding the following line to your vimrc file: map <S-j> 10j

However you'd be overwriting the useful existing J command (join two lines). Finding a well positioned unused key combination for 10j/10k might be difficult, so I suggest using the existing movements that I mentioned.

You may also want to know that you can move backwards to a word that you see by doing: ?someword and forward to a word you see by doing /someword. These are going to be faster than trying to move up/down 10 lines and then repositioning your cursor to the exact location. If you cant think of a simple search string for the line in question, you can always go to the line number as you said (xgg).

I was messing with vim and I noticed - moves you up and + moves you down, so you can:

10-

or you could use k since you're most likely used to hjkl cursor movement.