19

Sometimes I create macros. Sometimes I create the same macro multiple times.

Sometimes I get tired of creating the same macro over again. Can I store macros? Can I load a preset of macros? Can I make a macro and save it forever?

guntbert
  • 1,245
  • 1
  • 13
  • 27
sensorario
  • 523
  • 4
  • 14

1 Answers1

19

Yes, you can! There are a couple ways to do this. By default, all registers will be saved into your viminfo file, and loaded once you start vim. This is the easiest way. However, it's not foolproof. Each register will be lost if you accidentally record/yank over it.

The better way to save a specific macro is to put it in your .vimrc. For example, let's say you want foo to be saved into macro a. This could be achieved with:

let @a='foo'

in your vimrc. If you have your macros already made, you could type

ilet @a='<C-r><C-r>a'<esc>

to paste the current contents of the macro, if you don't feel like typing it all out again.


Thanks to Andrew Keeton for pointing out <C-r><C-r>

DJMcMayhem
  • 17,581
  • 5
  • 53
  • 85