How to write a macro in Vim

Macros are amazing but you have no idea what they are and why they are amazing. Well let's change that in a few minutes.

Macro is essentialy a set of instructions which you "record" (just like you record your voice for example) and then you run it (play that record). The thing is you can run them as many times you want in a row what makes them very powerful. So how to do it?

You start in "NORMAL" mode. To start recording a macro you press "q" key and then a key from a-z which you want to macro store under. Yes, you can record multiple macros and then run them arbitrarily. So let's say you name your macro "a" so press "q" and then "a" key. You will see in right bottom conrner that vim tell you you are "recording @a" ("@" is symbol for macro.

From now whatever sequance of keys you press is recorded until you get backto NORMAL model (Esc key) and then pres "q" to stop recording your macro. So now you can create macro with any functionality.

Last thing you need to know is how to run such macro. If you named your macro "a" you can run it with "@" key and then "a" where "@" stands for macro and "a" key for the macro name. You can also re-run your last maro run with "@@" key. But probably the most interesting thing is to run a macro multipletimes in a row. You can do that in classic vim way where you press "10@d" which means runs 10 times my macro called "a".

For more info about macros head to the official documentation page. I hope macros serves you good.