This time I’ll show you how to undo, redo and repeat stuff in Vim.
With just a few simple keystrokes you can improve your daily workflow.
Undo things in Vim is quite easy, as you can just press the u key in normal command mode.
The u key will navigate through the history of your changes. This means everytime you press it, one more change will be undone. You can also use a quantifier, such as 3u (undo the last 3 changes).
Please note that the u is lowercase. If you use an uppercase U instead, the whole last line you’ve modified will be reverted to its original state. However, U will not navigate through the undo history, but instead of it create a new undo history entry.
Redo & simple repeating
When we talk about “redo” things, we’ve to differ between the following two key strokes in normal command mode:
- . will repeat the last change you’ve done
- Ctrl-r will redo (revert) a previous undone change
Here are some examples for both commands.
Let’s say you’re switching to insert mode (i), press the comma (,) key and then go back to normal mode (ESC). You just inserted a comma under your cursor – that’s your change! Now you go a line down and want to do the same thing. At this point, you can simply press the . key, and the last change (insert a comma) is repeated.
Now switch to a different example where you did some changes you want to undo. So you press u several times, until you realize you did one undo too much. What you want to do now is redo your undo change, and this is where you’d use Ctrl-r.
We already know the repeat (aka .) command, our useful little helper. But what about more complicated scenarios, where you want to repeat a series of commands instead of only one? Well, this is where Vim macros come in place.
A macro is simply a sequence of commands. Macros can be recorded by using the following steps:
- First press the q key (macro key)
- Choose the macro register by pressing one of the a-z keys
- Enter your (complicated) Vim commands you want to record
- Finally press the q key again
Now you’ve recorded your series of commands in a macro and stored it in a register. Whenever you start recording a macro, you should see something like this on the bottom margin of Vim:
Let’s say I want to do this:
- Add the letter X right before the last character of the current line
- Go to the next line
- Jump to the first character
I’d use the following key combination:
- qq is starting the macro recording on register q
- $ is jumping to the last character in line
- i is switching to insert mode
- X is the letter X
- <ESC> is switching back to normal mode
- j (or alternatively down cursor) is jumping to the next line
- ^ is jumping to the first character in line
- q is stopping the macro recording
Of course a macro is only worth something if you can play / repeat it. This is where the @ key comes into place.
To play your macro simply press the @ key, followed by the register you’ve chosen before. In the example above, you can see my register was q, so I simply repeat it by pressing @q. You can also specify a qunatifier, such as 3@q (repeat macro q 3 times).
Appending to macros
If you want to append something to an existing macro, simply use the uppercase letter of your register. With that in mind:
- qx…q will replace the macro on register x
- qX…q will append to the macro on register x
Displaying all registers
To display all existing registers, simply use the following command-line command:
You can also display a single register by specifying it after the :reg[isters] command.
More about macros
More about macros, such as editing & saving them can be found on this wiki page.