This time I’ll present you tips on how to undo, redo and repeat stuff in Vim.
With just some easy keystrokes you’ll be able to enhance your every day workflow.
Undo issues in Vim is kind of straightforward, as you’ll be able to simply press the u key in regular command mode.
The u key will navigate via the historical past of your modifications. This means everytime you press it, yet another change can be undone. You can even use a quantifier, resembling 3u (undo the final 3 modifications).
Please notice that the u is lowercase. If you employ an uppercase U as an alternative, the entire final line you’ve modified can be reverted to its unique state. However, U is not going to navigate via the undo historical past, however as an alternative of it create a brand new undo historical past entry.
Redo & easy repeating
When we speak about “redo” issues, we’ve to vary between the next two key strokes in regular command mode:
- . will repeat the final change you’ve executed
- Ctrl-r will redo (revert) a earlier undone change
Here are some examples for each instructions.
Let’s say you’re switching to insert mode (i), press the comma (,) key after which return to regular mode (ESC). You simply inserted a comma underneath your cursor – that’s your change! Now you go a line down and wish to do the identical factor. At this level, you’ll be able to merely press the . key, and the final change (insert a comma) is repeated.
Now swap to a unique instance the place you probably did some modifications you wish to undo. So you press u a number of occasions, till you understand you probably did one undo an excessive amount of. What you wish to do now’s redo your undo change, and that is the place you’d use Ctrl-r.
We already know the repeat (aka .) command, our helpful little helper. But what about extra sophisticated situations, the place you wish to repeat a collection of instructions as an alternative of just one? Well, that is the place Vim macros are available in place.
A macro is solely a sequence of instructions. Macros could be recorded by utilizing the next steps:
- First press the q key (macro key)
- Choose the macro register by urgent one of many a-z keys
- Enter your (sophisticated) Vim instructions you wish to document
- Finally press the q key once more
Now you’ve recorded your collection of instructions in a macro and saved it in a register. Whenever you begin recording a macro, it’s best to see one thing like this on the underside margin of Vim:
Let’s say I wish to do that:
- Add the letter X proper earlier than the final character of the present line
- Go to the following line
- Jump to the primary character
I’d use the next key mixture:
- qq is beginning the macro recording on register q
- $ is leaping to the final character in line
- i is switching to insert mode
- X is the letter X
- <ESC> is switching again to regular mode
- j (or alternatively down cursor) is leaping to the subsequent line
- ^ is leaping to the first character in line
- q is stopping the macro recording
Of course a macro is simply value one thing for those who can play / repeat it. This is the place the @ key comes into place.
To play your macro merely press the @ key, adopted by the register you’ve chosen earlier than. In the instance above, you’ll be able to see my register was q, so I merely repeat it by urgent @q. You can even specify a qunatifier, resembling 3@q (repeat macro q 3 occasions).
Appending to macros
If you wish to append one thing to an current macro, merely use the uppercase letter of your register. With that in thoughts:
- qx…q will change the macro on register x
- qX…q will append to the macro on register x
Displaying all registers
To show all current registers, merely use the next command-line command:
You can even show a single register by specifying it after the :reg[isters] command.
More about macros
More about macros, resembling modifying & saving them could be discovered on this wiki web page.