Does anyone know how to properly save/reuse macros recorded inside of a vim editor?
Use q
followed by a letter (for example 'x') to record a macro, then press q
to end the macro definition. The pattern surrounded by the starting and ending 'q'
during this definition just goes into one of the copy/paste registers (in this case, register 'x') so you can paste it with the"xp
or "xP
commands in normal mode, where x is the register to paste. Typing "xp
in normal mode inserts the contents in register x and exits back to normal mode.
To save it, you open up .vimrc and paste the contents while defining the macro using let @x
, then the register will be around the next time you start vim.
The format is something like:
let @q = 'macro contents'
Be careful of quotes, though. They would have to be escaped properly.
So to save a macro 'x'
, you can do:
From normal mode: qx
enter whatever commands
From normal mode: q
open .vimrc
insert a line let @x = '<Ctrl-R><Ctrl->Rx'
Actually press Ctrl-R Ctrl-R x
to insert the contents of the x
register literally.
(H/T LAFK)
Although if you prefer, you can use "+x
from normal mode to paste into the string.