如何重复 Vim 中的最后 n 个更改?

发布于 2024-11-19 12:04:41 字数 108 浏览 2 评论 0原文

执行 . 会重复上次更改。执行 2. 会重复上次更改两次。

但想象一下我想在最后一个更改之前重复更改。我如何在 Vim 中做到这一点?

Doing . repeats the last change. Doing 2. repeats the last change two times.

But imagine I want to repeat the change before the last one. How do I do it in Vim?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

享受孤独 2024-11-26 12:04:42

根据 Fredrick Phil 的回答,这里有一个示例:

录制宏

下面演示了如何录制宏以删除带引号的字符串中的所有内容(包括带引号的字符串)并存储在寄存器 d 中。删除字符串的命令是 da"。因此,要将这个命令存储在宏寄存器 d 中,我们可以简单地执行以下操作:

qdda"q

注意它以 q 开头和结尾。第二个字符是寄存器,在此case d 表示删除。但我们可以给它任何字母或数字,剩下的字符 da" 是我们的命令。

使用我们的宏

现在我们的宏已被录制,我们可以通过使用 @ 符号后跟寄存器来调用它:

@d

重复上一个宏命令

要使用最近调用的宏命令再次:

@@

无关信息:

在本例中,我们使用了da",它代表删除带引号的字符串。(如果您想删除所有内容 引用的字符串内,但不是引号本身,您可以使用 di" 代替。)。

Based on Fredrick Phil's answer, here is an example:

Recording your macro

The following shows how to record a macro to delete everything in and including a quoted string and store in register d. The command to delete a string is da". So to store this command in macro register d we can simply do this:

qdda"q

Notice it starts and ends with a q. The second character is the register, in this case d for delete. But we could have given it any letter or number. The remaining characters da" is our command.

Using our macro

Now that our macro is recorded we can invoke it by using the @ symbol followed by the register:

@d

Repeating the last macro command

To use the most recently invoked macro command again:

@@

Unrelated info:

In this example, we used da" which stands for delete a quoted string. (If you instead wanted to delete everything inside the quoted string, but not the quotation marks themselves you can instead use di" instead.).

与酒说心事 2024-11-26 12:04:42

记录您的“编辑”

是的!你可以在 vim 中做到这一点!

Record Your "Edits"

yes! you can do this in vim! ????

One of Vim's most useful features is its ability to record what you type for later playback. This is most useful for repeated jobs that cannot easily be done with .

To start recording

  • press q in normal mode followed by a letter (a to z)
    • That starts recording keystrokes to the specified register. Vim displays recording in the status line
    • Type any normal mode commands, or enter insert mode and type text

To stop recording

  • ending in normal mode, come to normal mode if you are not, and press q
  • ending in insert mode, press Ctrl+O, this will temporarily get you into normal mode, and then press q

To playback your keystrokes/recording

  • press @ followed by the letter previously chosen
  • Typing @@ repeats the last playback

References

浅唱ヾ落雨殇 2024-11-26 12:04:41

您不认为可以,请参阅 :help 。 但是,您可以做的是为您的编辑录制宏,您有很多寄存器可供选择 {0-9a- zA-Z"}(大写以追加)。
然后使用例如 @u 来编辑 1,使用 @t 来编辑 2,依此类推。

Best of VIM Tips 有关录制的精彩提示,

" Recording (BEST TIP of ALL)
qq  # record to q
your complex series of commands
q   # end recording
@q to execute
@@ to Repeat
5@@ to Repeat 5 times
qQ@qq                             : Make an existing recording q recursive *N*
" editing a register/recording
"qp                               :display contents of register q (normal mode)
<ctrl-R>q                         :display contents of register q (insert mode)
" you can now see recording contents, edit as required
"qdd                              :put changed contacts back into q
@q                                :execute recording/register q

请查看以下内容以获取更多重复提示:

:&     last substitute
:%&    last substitute every line
:%&gic last substitute every line confirm
g%     normal mode repeat last substitute
g&     last substitute on all lines
@@     last recording
@:     last command-mode command
:!!    last :! command
:~     last substitute
:help repeating

Don't think you can, see :help . However, what you can do is to record a macro for your edits, you have a lot of registers to choose from {0-9a-zA-Z"} (uppercase to append).
Then use e.g. @u for edit 1, @t for edit 2 and so on.

Great tips about recording from Best of VIM Tips

" Recording (BEST TIP of ALL)
qq  # record to q
your complex series of commands
q   # end recording
@q to execute
@@ to Repeat
5@@ to Repeat 5 times
qQ@qq                             : Make an existing recording q recursive *N*
" editing a register/recording
"qp                               :display contents of register q (normal mode)
<ctrl-R>q                         :display contents of register q (insert mode)
" you can now see recording contents, edit as required
"qdd                              :put changed contacts back into q
@q                                :execute recording/register q

Have a look at these for more hints for repeating:

:&     last substitute
:%&    last substitute every line
:%&gic last substitute every line confirm
g%     normal mode repeat last substitute
g&     last substitute on all lines
@@     last recording
@:     last command-mode command
:!!    last :! command
:~     last substitute
:help repeating
失去的东西太少 2024-11-26 12:04:41

我编写了RepeatLast.vim 插件来满足这个确切的要求。它提供 5\. 键绑定来重复最后 5 个更改(包括移动),并提供 2\D 来删除/忘记最后 2 个操作。

它的工作原理是始终启用宏录制,这可能并不适合所有人。但如果您能接受这一点,它就适用于 99% 的用例。

最新版本: https://github.com/joeytwiddle/RepeatLast.vim (请反馈!)

注意事项:

:set ch=2,这样第一行输出就不会被“录音”消息隐藏。

有 1% 的情况无法按预期工作通常是由于:

  1. 难以缓慢触发 CursorHold 事件而不丢失
    快速重复击键
  2. 不受欢迎的[空格]录音和
    当用户响应提示时,按 [Enter] 键。

从长远来看,训练您的 q 肌肉来先发制人地记录宏可能是更好的方法。 ;-)

I wrote the RepeatLast.vim plugin to address this exact requirement. It provides a 5\. key binding to repeat the last 5 changes (including movements) and 2\D to drop/forget the last 2 actions.

It works by enabling macro recording all the time, which may not be desirable for everyone. But if you can live with that, it works in 99% of use cases.

Latest version: https://github.com/joeytwiddle/RepeatLast.vim (Please feedback!)

Caveats:

Please :set ch=2 so that the first line of output won't be hidden by the "recording" message.

The 1% of times it fails to work as intended are usually due to:

  1. Difficulties triggering the CursorHold event slowly without losing
    fast-repeated keystrokes
  2. Undesirable recording of [Space] and
    [Enter] keys when the user is responding to a prompt.

Training your q muscle to pre-emptively record macros might be a better approach in the long term. ;-)

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