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

发布于 2024-10-16 13:30:22 字数 135 浏览 5 评论 0原文

有没有办法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦途 2024-10-23 13:30:23

是的,当然有办法。 jk 向下和向上移动一行,因此 10j10k 向下和向上移动十行。您可以通过在其前面添加数字来重复任何动作。

如果您经常这样做,您可能还需要设置relativenumber - 它将通过打印相对于当前行的行号而不是绝对数字来帮助您节省计数。

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.

何以笙箫默 2024-10-23 13:30:23

上下移动 10 行可能不适合您的任务以及其他选项。考虑其他移动:

CtrlfCtrlb 向前和向后翻页。

}{ 向前和向后移动一段。

您可以在 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, 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).

季末如歌 2024-10-23 13:30:23

我正在搞乱 vim,我注意到 - 使你向上移动,而 + 使你向下移动,所以你可以:

10-

或者你可以使用 k 因为你最有可能习惯于 hjkl 光标移动。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文