How to: Store multiple Vim commands in macros and recall them

Updated . Posted . Visible to the public.

Vim allows recording a batch of commands as a macro. This is handy if you need to do the same things over and over.

Here is how:

  1. Press q to enter macro mode.
  2. Press a letter (not a number!) key to assign a slot to your macro.
  3. You are now recording. Do whatever you want with usual commands.
  4. Once you are done, press q again to stop recording.
  5. You can now run your recorded macro by pressing @ and its assigned letter key.

Cheats:

  • If you want to run a macro repeatedly, type a number before pressing the @ key. Example: 23@a will repeat macro "a" 23 times.
  • Press @@ to repeat the last macro. This does not work instantly after recording, you need explicitly call the macro once.
  • You can combine the above: 42@@ calls the last run macro 42 times.

If you want to repeat only the last command, you don't need a macro. Just press the . (period).

Profile picture of Arne Hartherz
Arne Hartherz
Last edit
Keywords
record, command, queue
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2011-08-02 08:41)