Macro is a complex type of repeation, it allow you to record and replay a sequence of actions.
A macro can be created by:
q<register-name>to start record mode and store the macro to the
<register-name>. For example
- Perform the sequence of edits or commands.
- Stop the record by hitting
At this point, you can replay a macro by pressing
@<register-name> (for example:
@a). To replay the macro multiple times, press
<n>@<register-name> for example
99@a will replay 99 times.
You can also call a macro while recording, this is called recursive macro.
Let’s use macro to convert a CSV table into SQL statements. For example, the following data table has 3 columns:
Kay Mitchell,firstname.lastname@example.org,2 Micah Christensen,email@example.com,1 Shad Carver,firstname.lastname@example.org,4 Chanda Benton,email@example.com,8 Darryl Joyner,firstname.lastname@example.org,4
We want to convert each line into an SQL statement that look like this. Only the name and email columns are used, the id will be deleted:
INSERT INTO users (name, email) VALUES ('<column 1>','<column 2>');
If we are not using macro, we have to repeat the following edit for each line:
0to jump to the beginning of line
ito enter Insert mode
INSERT INTO users (name, email) VALUES ('
- Then press
<C-o>f,to jump to the next
,character without exiting the Insert mode, type in the closing
<C-o>lto move left one character (skipping the
,) and insert the opening
<C-o>f,again to jump to the next
- Finally, press
<C-o>Dto delete whatever left on that line
To save time, hit
q. Then perform the above editing for the first line. When you are done, hit
Now, you can go to each line and press
@q to let the macro do its job.
If you don’t want to manually press
@q on each line, you can do it in the macro too! When recording your edit, at the last step, go to the next line and press
@q, then close the recording. This time, the macro will automatically jump to the next line and call itself again until the cursor reached the end of the file.
To see what’s your macro look like, open a new buffer and type
"qp, the content of the macro will be pasted to the buffer:
0iINSERT INTO users (name, email) VALUES ('^Of,'^Ol'^Of,');^OD
You can edit the macro as you want, and save it back to the
q register by pressing
"q to select the
q register, and
yy to copy the whole line).