Nvim Session 10: Macros & Repeat

The ultimate force multiplier. Record any sequence of edits, replay infinitely. The dot command for quick repeats.

Pre-Session State

  • Comfortable with motions and operators

  • Know text objects

  • Record and play macros

  • Edit macros

  • Use dot repeat effectively

Lesson 1: The Dot Command

Concept: . repeats the last change.

Exercise 1.1: Basic dot repeat

daw      " Delete a word
.        " Delete another word
.        " And another

Exercise 1.2: Repeat with motion

daw      " Delete word
w        " Move to next word
.        " Delete it
w.       " Move and delete
w.       " Again

Exercise 1.3: Change and repeat

ciw      " Change inner word
NEW<Esc>
n        " Find next search match
.        " Apply same change
n.       " Find and change
n.       " Continue

Exercise 1.4: What counts as "last change"?

A "change" is: - Insert mode session (from i to <Esc>) - Delete operations (d, x, c) - Put operations (p, P) - Substitution (:s)

NOT a change: - Motions (w, j, etc.) - Yanks (y) - Visual selections - Searches

Exercise 1.5: Ideal dot workflow

Pattern: Make the change repeatable, then repeat.

/word<CR>    " Find first occurrence
ciwNEW<Esc>  " Change it
n.           " Next + change
n.           " Next + change

Lesson 2: Recording Macros

Exercise 2.1: Basic recording

qa       " Start recording to register 'a'
...      " Do things
q        " Stop recording

Exercise 2.2: Play macro

@a       " Play macro in register 'a'
@@       " Repeat last macro
3@a      " Play macro 3 times

Exercise 2.3: First macro

Transform lines:

hostname1
hostname2
hostname3

To:

server_hostname1
server_hostname2
server_hostname3
qa           " Start recording to 'a'
Iserver_     " Insert at start
<Esc>j       " Exit insert, move down
q            " Stop recording
2@a          " Apply to next 2 lines

Exercise 2.4: Macro design principles

  1. Start in known state (beginning of line, specific column)

  2. End positioned for next iteration (usually next line)

  3. Use relative motions (w, j) not absolute

  4. Avoid line-specific searches (make patterns generic)

Lesson 3: Advanced Macro Patterns

Add quotes around all IP addresses:

server1 10.0.0.1 active
server2 10.0.0.2 standby
server3 10.0.0.3 backup
qa
/\d\+\.\d\+\.\d\+\.\d\+<CR>   " Find IP pattern
ciW"<Ctrl-r>""<Esc>           " Surround with quotes
q
@a@a      " Repeat for remaining

Exercise 3.2: Macro on visual selection

" Select lines
Vjj
:normal @a    " Run macro 'a' on each line

Or:

:'<,'>normal @a

Exercise 3.3: Global with macro

:g/pattern/normal @a    " Run macro on all matching lines
:g/TODO/normal @a       " Transform all TODOs

Exercise 3.4: Recursive macro

Macro that calls itself:

qaq          " Clear register 'a'
qa           " Start recording
...edits...
j            " Move to next line
@a           " Call itself!
q            " Stop recording
@a           " Start - runs until error/end

Warning: Stops when motion fails (end of file, search not found).

Lesson 4: Editing Macros

Exercise 4.1: View macro

:reg a       " View register 'a'
"ap          " Paste macro as text

Exercise 4.2: Edit macro text

" Paste macro
"ap

" Edit the text (it's just keystrokes!)
" Example: Iserver_ becomes IServer_

" Yank back to register
0"ay$

" Test new macro
@a

Exercise 4.3: Create macro from text

" Write the keystrokes directly
:let @a = 'Iprefix_^[j'    " ^[ is Ctrl-V Esc

" Or yank from buffer
"ayy

Exercise 4.4: Append to macro

qa        " Record to 'a'
...
q
qA        " Append to 'a' (uppercase!)
...more...
q
@a        " Plays original + appended

Lesson 5: Practical Macro Examples

Exercise 5.1: CSV transformation

Convert:

name,email,role
john,john@example.com,admin
jane,jane@example.com,user

To SQL:

INSERT INTO users VALUES ('john', 'john@example.com', 'admin');
INSERT INTO users VALUES ('jane', 'jane@example.com', 'user');
qa
IINSERT INTO users VALUES ('<Esc>
f,s', '<Esc>
f,s', '<Esc>
A');<Esc>
j0
q

Exercise 5.2: Log extraction

Extract usernames from:

2026-03-18 10:00 user=alice action=login
2026-03-18 10:05 user=bob action=login
2026-03-18 10:10 user=charlie action=logout
qa
/user=<CR>
wdW
I<Esc>
j0
q

Exercise 5.3: Config generation

Generate numbered config:

server1

Expand to:

server1
server2
server3
...
server10
qa
yy
p
<Ctrl-a>    " Increment number
q
9@a         " Repeat 9 times

Exercise 5.4: Markdown to HTML

Convert:

# Title
## Section
### Subsection

To:

<h1>Title</h1>
<h2>Section</h2>
<h3>Subsection</h3>
qa
0f#
:s/^### \(.*\)/<h3>\1<\/h3>/<CR>
j
q

(Simplified - real version needs conditionals)

Lesson 6: Dot + Macro Workflow

Exercise 6.1: When to use dot vs macro

Use dot (.): - Simple repeated change - One or two operations - Interactive "find and change"

Use macro: - Complex multi-step edits - Need to store for later - Many operations per iteration

Exercise 6.2: Combined workflow

" First, make the change
ciwNEWVALUE<Esc>

" Now repeat with dot
n.n.n.

" Or for complex case, record
qa
n.
q
100@a    " Repeat lots

Exercise 6.3: Count-controlled repetition

" Change word, move to next, repeat
ciwnew<Esc>w
100.      " Won't work - dot doesn't include motion!

" Instead:
qa
ciwnew<Esc>w
q
100@a     " Works!

Macro Reference

Command Effect

q{a-z}

Start recording to register

q

Stop recording

@{a-z}

Play macro

@@

Repeat last macro

{n}@a

Play macro n times

q{A-Z}

Append to macro

:reg a

View macro contents

"ap

Paste macro as text

Dot Command Reference

Command Effect

.

Repeat last change

{n}.

Repeat n times

Macro Design Checklist

  1. [ ] Start at known position (beginning of line/word)

  2. [ ] Use generic motions (w, j, f{char})

  3. [ ] End positioned for next iteration

  4. [ ] Test on one line before mass apply

  5. [ ] Use :normal @a for visual selection

Exercises to Complete

  1. [ ] Record a macro to add prefix to lines

  2. [ ] Use @a and @@ to repeat it

  3. [ ] Edit a macro by pasting and re-yanking

  4. [ ] Apply macro to visual selection with :normal @a

  5. [ ] Practice dot repeat with ciw and n.

What’s Next?

You’ve completed the core Neovim Editor Mastery curriculum!

Continue learning:

  • Vim Codex - Reference material

  • Lua Training - Configure Neovim

  • Explore domus-nvim plugins for extended text objects

Session Log

Timestamp Notes

Start

<Record when you started>

End

<Record when you finished>

Discoveries

<What surprised you?>