How do I convert a recorded macro into a permanent mapping?
Answer
:nnoremap <leader>x :norm! @a<CR>
Explanation
Once you've perfected a macro by recording and testing it, you can make it permanent by converting it into a mapping in your vimrc. This turns a temporary recording into a reusable keystroke shortcut available in every session.
How it works
- Method 1: Map to execute the macro register:
:nnoremap <leader>x :norm! @a<CR> - Method 2: Inline the keystrokes:
:nnoremap <leader>x dwwP(the actual keys) - Method 3: Store in register via vimrc:
let @a = 'dwwP'then map@a norm!(with bang) avoids recursive mapping issues
Example
" Record a useful macro
qa dw ww P q
" View the recorded keystrokes
:echo @a
" Output: dwwP
" Convert to permanent mapping
nnoremap <leader>sw dwwP
" Or keep it as a stored macro
let @a = 'dwwP'
nnoremap <leader>sw @a
Tips
"appastes macro contents — copy these keystrokes into your mapping- For complex macros with special keys, use
let @a = "keys\<Esc>more\<CR>" - Test the mapping before committing to vimrc
- Use
<Plug>mappings for plugin-style reusable macros