30 December 20
Vim’s macros can be very powerful when editing text, and the great part about them is that they are so easy to create on the fly. In contrast, if you have ever used microsoft excel’s macro features, you know that creating one macro requires knowledge of VBA (visual basic for applications).
Sure, you could just copy an example off the internet, but creating anything specific requires real knowledge. In vim however, you mainly just need normal mode editing knowledge to use macros effectively.
To record a macro, in normal mode, hit q, followed by any
key to name the macro. After that, you just need to execute what you
want to do, and end the macro with q. To call the macro, type
@
followed by the name you gave the macro.
A sample session:
qqdt.j0q
Which names a macro q
that deletes to the full stop,
goes down one line and puts the cursor at the start of the line. And to
execute:
@q
And if you wish to execute the macro say 20 times:
20@q
However, if you wish to run your macro recursively, you need to
change your macro to call the macro that was called last using
@@
:
qqdt.j0@@q
Which will repeat the macro all the way to the end of the file. This
application of macros is especially useful when you bulk rename files
with vidir
.
Best Regards,
Philip