Vim - 一次移动多行时跳回到之前的位置

Vim - jump back to previous position when moving multiple lines at once

当我使用Vim时,我经常在移动线条时使用动作命令。
例如,如果我想向下移动 20 行,我按 20j
现在在 "jumped" 20 行之后,如果我想再次回到我以前的位置,我必须输入 20k.

有没有一种方法可以在不输入 20k 的情况下跳转到我之前的位置?
例如,通过某种方式将之前的位置添加到 Vim 的跳转列表中,然后我可以使用 <c-o> 向后跳转。

(顺便说一句,我只想在一次移动多于一行时跳回)。

这里的问题是 jk 不是 "jumps"。当您执行 20j 时,您实际上是在执行 jjjjjjjjjjjjjjjjjjjj 但速度非常快,因此您必须将这些任意动作转换为适当的跳跃才能使 <C-o> 起作用。 :help jumplist:

中解释了如何执行此操作

You can explicitly add a jump by setting the ' mark with "m'".

实践中:

m'20j

然后 <C-o>''`` 返回。

不过,还有更聪明的移动方式,不需要您计算行数,而是实际的跳跃,例如 :help /:help ?

我的 ~/.vimrc 文件中有以下内容:

" It adds motions like 25j and 30k to the jump list, so you can cycle
" through them with control-o and control-i.
" source: https://www.vi-improved.org/vim-tips/
nnoremap <expr> j v:count ? (v:count > 5 ? "m'" . v:count : '') . 'j' : 'gj'
nnoremap <expr> k v:count ? (v:count > 5 ? "m'" . v:count : '') . 'k' : 'gk'

在我的例子中,大于 5 行的行移动被添加到跳转列表中。