Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
820 views
in Technique[技术] by (71.8m points)

emacs - How can I save evil-mode (vim style) macros to my init.el?

I'm using evil-mode, after moving over from Vim about a year ago. I've made a number of customisations, but not yet worked out how to save vim-style keyboard macros.

I can define and run them in evil-mode, using the exact same keys as in Vim.

  • qa letter to start recording a macro.
  • q to stop recording.
  • @a letter to run the macro.

I'd like to save some of these to my init.el file to use between sessions, but I've not been able to find any reference to this online, and haven't found the right syntax.

How can I save these evil-mode keyboard macros to file? Is there some reason I should be using native Emacs keyboard macros instead, and is there a way I can run these via @a key?

question from:https://stackoverflow.com/questions/22817120/how-can-i-save-evil-mode-vim-style-macros-to-my-init-el

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Evil-mode macros are not special, they are just ordinary Emacs macros and you save them the same way, but you'll need to do some special work to get them into the evil registers.

Let's walk through an example:

In a buffer, do qfifoobarESCq. This will save a macro into the f register that inserts foobar into the buffer.

Now run M-xname-last-kbd-macroRETmymacroRET.

Go to your init.el file and do M-xinsert-kbd-macroRETmymacroRET.

This will dump your macro out into an fset call.

(fset 'mymacro [?i ?f ?o ?o ?b ?a ?r escape])

If you place this in your init.el you will have access to the command mymacro from M-x.

But, we saved this into register f and we want it to be there at each startup. You need to extract the macro vector from the code above and save that to a register in your init.el like this:

;; make sure this is done after evil-mode has been loaded
(evil-set-register ?f [?i ?f ?o ?o ?b ?a ?r escape])

Now you will have access to it from @!

See the docs about naming and inserting macros as text


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...