vimtricks.wiki Concise Vim tricks, one at a time.

How do I run text I've yanked or typed as a Vim macro without recording it first?

Answer

@"

Explanation

Vim macros are stored in registers — and you can execute any register as a macro with @{register}. Since yanked or deleted text lands in the unnamed register ("), you can compose a macro directly as text, yank it, then run it immediately with @". This is especially powerful for one-off or iterative macros that are easier to write and edit than to record keystroke by keystroke.

How it works

  • @" — execute the contents of the unnamed register as a normal-mode macro
  • The unnamed register " holds the last yanked or deleted text
  • Any register works: @a, @b, etc. — " just makes yank-and-run frictionless
  • @@ re-runs the last executed macro (regardless of which register it came from)

Example

Suppose you want a macro that wraps the word under the cursor in double quotes. Instead of recording with q, write the keystrokes as literal text in your buffer:

ea"<Esc>bi"<Esc>

Yank the line with yy. Now @" executes it on any word. If the result is wrong, edit the text, yank again, and re-run — no need to re-record.

Tips

  • Combine with "ayy to move a macro into a named register: yank from unnamed into a by running "ayy on the macro text, then use @a for repeated calls
  • " can also be populated by deletes: delete a line of commands with dd, then @" runs it
  • This technique is the foundation of editable macros: record once, print with :put a, fix the text, yank back with "ayy, replay with @a

Next

How do I apply a normal mode command to every line in a range at once?