How do I repeat the last n changes in Vim?

Viewed 12427

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?

4 Answers

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!

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

Related