brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: qa records a Vim macro and @a plays it back

Vim
~/tips/tip-vim-record-macro

qa

Records a series of keystrokes once and repeats it on other lines.

When you need the same edit on many lines, but it is too fiddly for a substitute, record it once and let Vim replay it.

qa
A;<Esc>j
q

qa starts recording into register a. Everything you type is stored: here A; adds a semicolon at the end of the line, Escape leaves insert mode, and j moves down. The final q stops recording.

Now play it back:

@a
10@a
@@

@a runs it once, 10@a ten times, and @@ repeats the macro you ran last. Because the macro ends with j, every run leaves the cursor ready for the next line.

The trick to a reliable macro is to start and end in a predictable spot. I’d begin with 0 or ^ so the cursor position doesn’t matter, and use motions like f, or w instead of pressing l five times.

When a motion inside the macro fails, for example a search finds nothing or j hits the last line, Vim stops the whole run. That is actually useful: 999@a simply runs until it runs out of lines. Just keep an eye on what it does with u at hand.

Further reading: Vim help: recording, Vim help: @ register execution

Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more

More Vim tips :%s/<Ctrl-r><Ctrl-w>/newname/g /oldname find . -name "*.conf" | vim – vim --clean file.txt :set textwidth=72 All Vim tips (45) All topics

Link