This is a way how to properly save/reuse macros recorded inside of a vim editor.
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.
To save it, you open up .vimrc and paste the content, then the register will be around the next time you start vim. The format is something like:
let @q='macro content'
- From normal mode:
qq
- enter whatever commands
- From normal mode:
q
- open .vimrc
- "qp to insert the macro into your
let @q='...'
line Maybe it is better to useCTRL+R CTRL+R q
, to insert register content without interpreting it.
To insert special characters (like escape, ...), use CTRL-V <ESC>
.
More examples:
:let @a="iHello World!\
You must use double quotes to be able to use special keys like in \<Esc>
.
How to execute the current line as VIM command ?
To execute the current line as an ex command, you may use:
yy:@"
- Start recording macro:
qq
- do whatever you want
- Stop recording macro:
q
- Paste it in current document:
<Shift+o>let @a='<CTRL+R><CTRL+R>q'
- Modify the macro the way you like it.
- Run it:
yy:@"
( oryy:<Ctrl+R>"<Backspace>
)
See: https://stackoverflow.com/questions/14385998/how-can-i-execute-the-current-line-as-vim-ex-commands/14386090