在 vi/vim 中将光标从当前位置移动 x 行
有没有办法在 vi/vim 中将光标移动相对行数?假设光标位于要删除的代码块下方 10 行处。如果您有按相对顺序显示的行号,那么最好有一个“向上跳转 10 行命令”来将您带到那里。
或者也许最好显示绝对行号并转到 xgg 其中 x 是行号?
Is there a way to move the cursor a relative amount of lines in vi/vim? Say you have the cursor 10 lines under a block of code you want to remove. If you have the line numbers shown in relative order, it would be nice to have a "jump 10 lines up command" that would take you there.
Or perhaps it's better to have the absolute line numbers shown and go xgg where x is the line number?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,当然有办法。
j
和k
向下和向上移动一行,因此10j
和10k
向下和向上移动十行。您可以通过在其前面添加数字来重复任何动作。如果您经常这样做,您可能还需要
设置relativenumber
- 它将通过打印相对于当前行的行号而不是绝对数字来帮助您节省计数。Yep, of course there's a way.
j
andk
move down and up one line, so10j
and10k
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.上下移动 10 行可能不适合您的任务以及其他选项。考虑其他移动:
Ctrlf、Ctrlb 向前和向后翻页。
}、{ 向前和向后移动一段。
您可以在 vimrc 中编写规则,将
10j
绑定到某个键,例如J
通过将以下行添加到 vimrc 文件中来向下移动 10 行:地图10j
但是,您将覆盖有用的现有
J
命令(连接两行)。为10j
/10k
找到一个位置良好的未使用按键组合可能很困难,因此我建议使用我提到的现有动作。您可能还想知道您可以通过执行以下操作向后移动到您看到的单词:
?someword
并通过执行/someword
转发到您看到的单词。这些比尝试向上/向下移动 10 行然后将光标重新定位到确切位置要快。如果您无法为相关行想出一个简单的搜索字符串,您可以随时按照您所说的那样转到行号(xgg
)。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, sayJ
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 for10j
/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
).我正在搞乱 vim,我注意到 - 使你向上移动,而 + 使你向下移动,所以你可以:
或者你可以使用 k 因为你最有可能习惯于 hjkl 光标移动。
I was messing with vim and I noticed - moves you up and + moves you down, so you can:
or you could use k since you're most likely used to hjkl cursor movement.