6) actually persists macros and named buffers automatically (by default, although I haven't looked for a way of turning this behavior off). Closing a Vim session will update the ~/. viminfo file with any named buffers / macros.
As you yank , delete , or create macros in vim, it automatically stores text into these registers. When you hit p paste it's simply pasting in the default register. You can also paste in any other register by hitting "qp where q is the register that you want to paste in.
Type "ayy to yank the modified macro into the a register. Type dd to delete the pasted register from the file you are editing.
To start recording, press q in normal mode followed by a letter ( a to z ). That starts recording keystrokes to the specified register. Vim displays recording in the status line. Type any normal mode commands, or enter insert mode and type text.
Use q
followed by a letter to record a macro. This just goes into one of the copy/paste registers so you can paste it as normal with the "xp
or "xP
commands in normal mode, where x is the register to paste.
To save it you open up .vimrc and paste the contents, 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 you can do:
qq
q
"qp
to insert the macro into your let @q = '...'
lineFor a more robust solution you can checkout Marvim.
It lets you save a macro in a specific namespace (or use the filetype as a default namespace) and you can later search for your saved macros and load them in a register ready for use.
If you reuse a lot of macros, this is pretty helpful.
Write your macros inside your ~/.vimrc, to define a macro launched by CTRL+O by example, add the following line to your ~/.vimrc :
map <C-O> MACROTEXT
when you record a macro by typing qa
you can retrieve your macro text by typing "ap
The :mkexrc
(or :mkvimrc
) command can be used to save all the current :map
and :set
settings to a file. See :help mkexrc
for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With