Answer
:let @q = substitute(@q, '\n$', 'A;<Esc>\n', '')
Explanation
Rerecording a long macro for one tiny change is slow and error-prone. Because macros are stored as plain text in registers, you can patch them directly with Vimscript. This lets you make surgical updates to an existing automation flow while preserving the rest of the sequence exactly as recorded.
How it works
@qaccesses the contents of registerq(your macro)substitute(..., '\n$', 'A;<Esc>\n', '')replaces the final newline terminator with extra keystrokes before restoring the newline:let @q = ...writes the modified result back into the macro register
Example
Assume register q already contains a macro that edits each line, but you now need it to append a semicolon at the end of every processed line.
:let @q = substitute(@q, '\n$', 'A;<Esc>\n', '')Now running @q reuses the original macro plus the appended A;<Esc> step.
Before: alpha
After: alpha;
Tips
- Use
:echo @qbefore and after to inspect exactly what changed - Keep a backup copy first, for example
:let @z = @q, so you can roll back instantly
category: macros tags: #macros #registers #automation #vimscript
Next
How do I debug a Vim macro one command at a time?
Related Tricks
How do I remove accidental Enter keystrokes from a recorded macro?
:let @q = substitute(@q, '\n', '', 'g')
How do I execute a macro from bottom to top over a selected range?
:'>,'<normal @q
How do I append keystrokes to a macro without re-recording it?
:let @q .= 'j'
How do I refactor a recorded macro by rewriting its keystrokes with substitute()?
:let @q = substitute(@q, 'foo', 'bar', 'g')
How do I append new keystrokes to an existing macro register without re-recording it?
:let @q .= 'A;<Esc>'
