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 registera- 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
:noremapfor a more permanent solution