Brand logoharrybaines.net

Vim macros in 5 minutes

Learn the magic of macros in Vim in 5 minutes.

Author profile
Harry Baines
Software Engineer
April 22, 2025
2 min read

Let's say we have the following list of todo items:

Set up project repository with initial README and license
Define project scope and create a feature roadmap
Implement user authentication and authorization
Set up CI/CD pipeline with automated tests
Design and build the core API endpoints
Write unit and integration tests for critical components

And we want to convert them all to todo list items in markdown to look like this:

- [ ] Set up project repository with initial README and license
- [ ] Define project scope and create a feature roadmap
- [ ] Implement user authentication and authorization
- [ ] Set up CI/CD pipeline with automated tests
- [ ] Design and build the core API endpoints
- [ ] Write unit and integration tests for critical components

We could do this manually going through each line and typing out the markdown checkbox, or use the mouse to select and copy paste, but using Vim macros we can do this much more efficiently.

Vim Macros

First, copy the following into a new file and open with vim:

Set up project repository with initial README and license
Define project scope and create a feature roadmap
Implement user authentication and authorization
Set up CI/CD pipeline with automated tests
Design and build the core API endpoints
Write unit and integration tests for critical components

Next, move the cursor to the start of the first line, and type:

qw

This will start recording a new macro into the register 'w'.

Then go into insert mode and type:

- [ ]

This will be the series of characters we will apply to the start of every line.

Go back to normal mode, and go to the start of the line below:

j0 (or shift+)

Then, stop recording with

q

Finally, we can do:

5@w

to run our macro against each line.

This is definitely one of my favourite vim tricks. If we had 50 lines of items, or a CSV of data, this trick is your best friend!