0

Possible Duplicate:
Saving vim macros

I always used macros across the lines and they're a lifesaver. But lately, I've started using them to do some more complicated file manipulations, and I would like to record them to a function, or use them in some way across Vim sessions so that I don't have to re-record them every time I quit Vim.

What would be the easiest way to go about that?

Community
  • 1
  • 1
Rook
  • 54,867
  • 44
  • 156
  • 233
  • @pb2q - I'd say relevant, but not a duplicate. But thanks for the link. it answered part of my inquiry. – Rook Jun 12 '12 at 21:27
  • yes, I see. You did specifically mention wrapping a function around your macro. – pb2q Jun 12 '12 at 21:38

2 Answers2

3

Vim macros are actually just text saved in registers, and since the registers are saved across sessions - your macros are automatically saved.

However, the way I see it, the whole idea behind macros is that you can create them quickly, use them, and then discard them. If you want something more permanent, you should take the effort and write a custom command or a keymap - and if that's a complex macro, you should write a function for it in proper vimscript(or whatever other scripting language vim supports).

If it's something you are going to use alot, you want to be able to make changes and fixes to it now and then - but this is very hard to do in macros, because they are text strings representing key strokes - not commands.

Idan Arye
  • 11,682
  • 3
  • 40
  • 61
  • I generally write macros that need a starting point from line 1, column 1 and then manipulate the file with that in mind. So far, that approach worked for me. So now, I'd just like to put them in a function and name that function and put a shortcut to the menu, so I can have those operations close by when I need them without the need to re-record macros. Tried vimscript, but this is so much easier that I don't feel like rewriting everything is worth the effort. – Rook Jun 12 '12 at 21:26
  • 1
    Well, as I said, macros are basically text. So I guess you can use the `:normal` command to create a function that runs a specific macro. Just make sure to escape all the special characters(it's a bad idea to store a special character directly in a text file) – Idan Arye Jun 12 '12 at 22:14
2
function! MyMacro()
   normal "paste your macro here
endfunction

But I agree with Idan Arye: making it a proper function written in vimscript is probably a better solution.

romainl
  • 158,436
  • 18
  • 236
  • 264