开发者

How do I repeat the last n changes in Vim?

Doing . rep开发者_高级运维eats 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?


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


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. ;-)


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.).


Record Your "Edits"

yes! you can do this in vim!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜