C.W.K.
Stream
Lesson 05 of 06 · published

Marks, Macros, and Registers

~15 min · vim, marks, macros, registers

Level 0Trapped
0 XP0/35 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

Marks — bookmarks for your buffer

A mark is a labeled position you can jump back to. ma sets mark a at the cursor; 'a jumps to that line; ` + a jumps to the exact column. Lowercase marks (az) are local to the file; uppercase marks (AZ) are global, useful for "jump-back-to-this-file-anywhere."

The marks Vim sets for you

  • ` + . — last edit position. (Hugely useful — "take me back to where I was just typing.")
  • ` + ^ — last Insert-mode position.
  • ` + [ / ` + ] — start / end of the last change or yank.
  • ` + < / ` + > — start / end of the last visual selection.
  • ` + ' — last jump position (`` works as toggle).

Macros — record and replay

A macro is a recorded sequence of keystrokes saved to a register. Press q + a register letter to start recording, do your edit, press q again to stop. Replay with @a, or @@ to replay the last macro. 10@a replays 10 times.

The macro recipe that always works

  1. Position the cursor at the start of a line.
  2. Start recording: qa.
  3. Do all the editing for one line, then end with j (or n after a search) so the cursor lands on the next target.
  4. Stop recording: q.
  5. Replay: @a for one more, 100@a for everything below.
Macros are dot on steroids. The dot command repeats one change. A macro repeats an entire sequence of changes plus the navigation between them. When the dot formula isn't enough — when you need a multi-step transform — record a macro instead.

Registers — Vim's many clipboards

Vim doesn't have one clipboard; it has dozens. Each register is named by a single character. Yanking with "ay puts the text in register a; pasting with "ap retrieves it. Some registers are special:

  • "" — the unnamed register (default for d/y/c/p).
  • "0 — the yank register; contains your last yank only (deletes don't touch it). Pasting from "0p gets your last yank back even if you've deleted stuff since.
  • "+ — the system clipboard (Cmd+C / Ctrl+C territory).
  • "* — the X11 selection clipboard (the middle-click one on Linux).
  • ". — last inserted text.
  • ": — last command-line.
  • "/ — last search pattern.
  • "_ — the black hole (writes go nowhere — useful for deleting without overwriting your yank).

Code

Marks·vim
ma         " set mark 'a' at cursor
'a         " jump to the line of mark 'a'
`a         " jump to the exact position of mark 'a' (line + column)
:marks     " list all marks
`.         " last edit position
`^         " last Insert position
`[  `]     " start / end of last change or yank
`<  `>     " start / end of last visual selection
''         " last jump position
:delmarks a    " delete mark 'a'
:delmarks!     " delete all lowercase marks in current buffer
Macros·vim
qa         " start recording into register 'a'
...        " do your editing
q          " stop recording
@a         " replay macro 'a' once
@@         " replay last-played macro
5@a        " replay 5 times
:5,20norm @a   " run macro 'a' on lines 5 through 20
qA         " append to existing macro 'a' (note: capital A)
Registers·vim
"ay       " yank to register 'a'
"ap       " paste from register 'a'
"+y       " yank to system clipboard
"+p       " paste from system clipboard
"0p       " paste your last YANK (ignoring deletes)
"_dd     " delete a line WITHOUT clobbering the yank register
:reg          " view all register contents
:reg a + 0   " view just registers a, +, and 0

External links

Exercise

(1) Set marks a and b at two distant lines and bounce between them with 'a and 'b. (2) Record a macro that converts foo = 1 into const foo = 1; on a single line and ends with j; replay it on 10 lines with 10@a. (3) Yank a word, then delete a different word with "_dw, then paste — verify the yank survived because you used the black-hole register.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.