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

How do I create a macro without recording it?

Answer

:let @a = "Iprefix: \<Esc>"

Explanation

The :let @a = "..." command sets the contents of a register directly. This lets you create macros by typing the keystroke sequence as a string.

How it works

  • :let @a = "commands" sets register a
  • Special keys use backslash notation: \<Esc>, \<CR>, \<C-a>
  • The macro can then be run with @a

Example

Create a macro that comments a line with //:

:let @c = "I// \<Esc>j"

Now @c adds // at the start of the line and moves down.

Tips

  • Use \<Esc> for Escape, \<CR> for Enter
  • Double-quoted strings interpret backslash sequences
  • Single-quoted strings are literal (no interpretation)
  • This is useful for sharing macros or putting them in your vimrc
  • Combine with :noremap for a more permanent solution

Next

How do you yank a single word into a named register?