179

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

jnadro52
  • 3,064
  • 3
  • 17
  • 16

6 Answers6

232

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

  • From normal mode: qq
  • enter whatever commands
  • From normal mode: q
  • open .vimrc
  • "qp to insert the macro into your let @q = '...' line
timss
  • 9,344
  • 3
  • 31
  • 54
Dan Olson
  • 21,303
  • 4
  • 37
  • 52
  • 3
    Also, if you need to insert special characters (like escape, in my case) use `CTRL-V ` in insert mode to insert the correct character. A literal `` doesn't work – adam_0 Jan 28 '14 at 01:33
  • 5
    "Note however, that the above method using :let will not work as expected for any macros which you make ending in a or character (carriage return or newline).". http://vim.wikia.com/wiki/Macros#Saving_a_macro – Paolo Jul 27 '14 at 21:09
  • 2
    Are the quotes (around macro contents) required? – user2472071 Nov 28 '14 at 19:43
  • 16
    `Ctrl+r Ctrl+r registerName` inputs register content without interpreting them - better than `"qp`. +1 for in-vim method nevertheless. – LAFK says Reinstate Monica May 31 '15 at 17:12
  • @adam_0 Could you elaborate on this? Perhaps, include an example? I can't figure out how to embed special characters, namely esc. I've tried every combination of inputting an escape to no avail. The "key code" ends up ruining the macro and the entire sequence gets printed. – GH05T Apr 02 '17 at 09:44
  • 1
    @GH05T for example, if you have `:set expandtab` so that hitting the key inserts a number of spaces, you can type (from insert mode) `CTRL-V ` to insert an actual tab character. – adam_0 Apr 06 '17 at 17:11
  • For me, I have `let @l='/, ai'` in my _vimrc, but when I try "@l" it just puts "/, ai" into the command bar at the bottom, rather than executing it as a macro. What am I doing wrong? – Neil Barnwell Oct 18 '17 at 15:11
  • I have MacOS 10.14.6 (Mojave) and I have found that recording macros using the 'q' results in the macro being persisted between sessions automatically. Or yanking to a named buffer also results in the yanked contents being available to subsequent vim sessions. I have found that the macros (or named buffers) are stored in ~/.viminfo. I am using vim 8.0. – Mark Aug 07 '20 at 14:16
21

For a more robust solution you can checkout Marvim.

It let's 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.

random
  • 211
  • 1
  • 2
  • 6
    Just want to second this and say how easy it is to setup marvim- copy a single file to your /plugin directory, then use F3 to save a macro and F2 to run. – Raine Revere Jun 08 '12 at 14:32
18

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

Raoul Supercopter
  • 4,985
  • 1
  • 31
  • 36
11

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.

Matthew Slattery
  • 41,297
  • 6
  • 92
  • 117
9

You can do like this on your ~/.vimrc

:let @a="iHello World!\<CR>bye\<Esc>"

NOTE: You must use double quotes to be able to use special keys like in \<this silly example>.

SergioAraujo
  • 8,735
  • 1
  • 46
  • 37
  • 1
    Thanks for this, saved me lots of headaches since my macro uses lots of ESC and the generated sequence for ESC key is something like `^[<80>a`, and tricky to make it work w/ single quotes. This approach is simpler/easier, and should be emphasized in the docs. – Ranel Padon Jan 04 '21 at 01:13
0

Vim 8.0 on MacOS Mojave (10.14.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.

Mark
  • 3,132
  • 1
  • 13
  • 21