vimeditormacros

Saving vim macros


Does anyone know how to properly save/reuse macros recorded inside of a vim editor?


Solution

  • 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: