Save and edit vim macros

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'
  1. From normal mode: qq
  2. enter whatever commands
  3. From normal mode: q
  4. open .vimrc
  5. "qp to insert the macro into your let @q='...' line Maybe it is better to use CTRL+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!\bye\"

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:


  1. Start recording macro: qq
  2. do whatever you want
  3. Stop recording macro: q
  4. Paste it in current document: <Shift+o>let @a='<CTRL+R><CTRL+R>q'
  5. Modify the macro the way you like it.
  6. Run it: yy:@" ( or yy:<Ctrl+R>"<Backspace> )


All Articles Bookmarks