How to write a macro in vim

Vim is my favourite text editor. It takes a bit of a learning curve to get used to its quirks but it is time well spent if you are doing lots of stuff on the command line or don’t have access to a GUI. I never used emacs so I won’t go into the editor war.

Macros in vim allow to record a sequence of commands and play them back in one go. Here is a cheat sheet adapted from vim tips:

Vim

1

2

3

4

5

qa start recording to registera

...enterasequence of commands

qstop recording

@aexecutethe macro

@@executethe macro again

Macros allow to automate complex sequences of vim commands and there is much more to it than this simple cheat sheet. Practical Vim dedicates a full chapter to the subject and it’s a good place to start if you want to delve into the magic world of vim macros.