Post

Vim Macros

Leverage the power of Vim Macros and automate repetitive text editing task in Vim.

As a developer, we adopt the philosophy of dry. “do not repeat yourself”. This is what sparked my interest in Vim macros, as macros enable us to automate the repetitive process of certain tasks.

Vim macros allow you to repeat a set of keystrokes with the one keyboard shortcut.

Vim Macros allow you to repeat a set of commands of your choosing.

Vim macros are recordings of vim commands which you can easily repeat back.

The way it works, is you record all of commands and then you can play that recorded command back.

Create a macro

start recording a new macro called “a”

1
qa

End recording / macro

to end a recording

1
q

Run a Macro

To run a recording type

1
@a

Execute a Macro multiple times

to repeat macro h 5 times

1
5@h

Execute the last Macro multiple times

repeat last macro 50 times

1
50@@

See vim macros usage example of adding a dash before each line

img-description vim macros usage example

Macro notes:

  • These recordings are stored in registers.
  • The recordings will be cleared when you close your buffer.
  • The recordings do not persist from buffer to buffer.
This post is licensed under CC BY 4.0 by the author.