How do I modify the contents of a register without re-recording it?
Answer
:let @a = substitute(@a, 'old', 'new', 'g')
Explanation
After recording a macro or yanking text into a named register, you may need to tweak it — fix a typo in a recorded macro, change a variable name in yanked text, or adjust a command sequence. Instead of re-recording from scratch, you can directly modify register contents using :let and Vim's string functions.
How it works
@aaccesses the contents of registeraas a stringsubstitute()works like:s///but on a string value instead of buffer text:let @a = ...writes the result back into registera- The
'g'flag replaces all occurrences, just like:s///g
Example
You recorded a macro in register q that changes foo to bar:
:echo @q
" Output: cwbar^[n
Now you need it to change foo to baz instead:
:let @q = substitute(@q, 'bar', 'baz', '')
:echo @q
" Output: cwbaz^[n
The macro is updated without re-recording.
Tips
- Use
:echo @ato inspect register contents before editing - You can also paste the register into a buffer with
"ap, edit it, then yank it back with"ayy - Other string functions work too:
:let @a = toupper(@a)or:let @a = @a . 'extra text' - To clear a register:
:let @a = ''