Macros in Vim¶
Table of Contents¶
- Recording a Macro
- Paste a Recorded Macro
- Repeating Macro
- Apply Macro to a Visual Selection
- Some keymaps
- tl;dr: Recursive Macros
- Recursive Macros (more detailed)
- Making a Numbered List Using Macros
- Misc Macro Notes
Recording a Macro¶
To record a macro in vim, just hit q
followed by another letter.
The other letter will be the register to record that macro into.
For example, qq
will start recording a macro into the register q
.
Paste a Recorded Macro¶
"qp
- Paste the macro recorded in the registerq
Repeating Macro¶
Q
: Repeat the last recorded macro.@@
: Repeat the last executed macro.
Apply Macro to a Visual Selection¶
Apply a macro to visual selection by calling norm
with the macro:
:'<,'>norm @q
Side Note: You can also use this method with
cdo
or any
other commands/keystrokes you want to apply to each line.
Some keymaps¶
vim.keymap.set('n', 'Q', '@qj')
vim.keymap.set('x', 'Q', ':norm @q<CR>')
tl;dr: Recursive Macros¶
To make a macro recursive, you'll need to nest append a macro call
to itself within the macro.
Here's how:
-
Create the first macro that you want to repeat
- Position my cursor where I want to make the first change
qa
- start recording into registera
- Make all my changes, doing it in a way that should apply cleanly to all locations
q
- stop recording- Position cursor on next location I want changed
@a
- run the macro to test and make sure it works as intended
-
Now append
@a
to that macro (so it calls itself)qA
- start recording to append to macro registera
- Move cursor to next location to change
@a
q
Now you can call @a
and it will repeat.
Recursive Macros (more detailed)¶
To create a recursive macro, you need to first record the macro you want to repeat.
For this example, we'll be using the a
key as a macro register.
- Press
q
then another key to start recording to that macro register.- e.g.,
qa
will record to thea
macro register.
- e.g.,
- Perform the actions that you want to repeat.
- Stop recording with
q
. - To make it recursive, you now need to append to the same macro register.
- To do this, press
q
and then<Shift>+(original_register)
- the capital of the key you chose. - Since we chose
a
, we'd enterqA
.
This appends to the macro in thea
macro register.
- To do this, press
- Now that you're appending, you want to enter the original macro (so the macro contains the
command to call itself). You only need to do this once.- Press
@a
to call the macro. - This calls the macro from within the macro, making it recursive.
- It's appending a macro call to itself to the macro.
- Press
- Exit macro recording with
q
. - Now, call your recursive macro with
@a
(assuming you chose thea
register)
Making a Numbered List Using Macros¶
- Create the first list entry, make sure it starts with a number.
qa
: start recording into register 'a'Y
: yank the entryp
: put a copy of the entry below the first oneCTRL-A
: increment the numberq
: stop recording<cnt>@a
: repeat the yank, put and increment<count>
times
Misc Macro Notes¶
-
A common macro could be
{!}fmt
(reformat the current paragraph with an external program). -
The
+
command line option to vi/Vim is normally used to open the file at a given line number.- But... The
+
can be followed by any valid Ex command/expression. - E.g., To remove an entry from my SSH known hosts file non-interactively:
vi +'/foo/d|wq!' ~/.ssh/known_hosts
- But... The
-
You can run files as pseudo-macros.
- We can apply that "macro" to a file by using a command like:
echo "{!}fmt" > mymacro.ex vim +'so mymacro.ex' ./mytarget
- We can apply that "macro" to a file by using a command like:
-
The
:@
Ex command executes the contents of a register as if it were a vi or ex command.:h :@
-
Use
:r!locate ...
to find some file on your system and read its name into your document.:h :r!