Tuesday, December 30, 2014

How to define a keyboard macro in Emacs

Link.


Here is how to define a keyboard macro: 
  • `C-x (’ or <f3>– start defining a keyboard macro
  • `C-x )’ or <f4>– stop defining the keyboard macro
And here is how to execute a keyboard macro you’ve defined: 
  • ‘C-x e’ or <f4> – execute the keyboard macro
Here’s how to execute the macro 37 times (you use ‘C-u’ to provide the 37): 
  • ‘C-u 37 C-x e’
Example:
You want to add the text “– foobar was here” at the end of each of the lines in a file. 
  1. Place the TextCursor at the beginning of the first line.
  2. `C-x (’ to start recording a keyboard macro.
  3. ‘C-e’ (or ‘M-x end-of-line’) to move the cursor to the end of the line.
  4. Type “-- foobar was here
  5. ‘C-a’ (or ‘M-x beginning-of-line’) to move the cursor to the beginning of the line.
  6. ‘C-n’ (or ‘M-x next-line’) to move the cursor to the beginning of the next line.
  7. `C-x )’ to stop recording the keyboard macro.
  8. ‘C-u 0 C-x e’ to execute the macro an infinite number of times until the end of the file is reached (See InfiniteArgument).
What’s important here is to stop defining the macro at a similar position to where you started it – so that you are in a position to execute it again, after it executes. This is a general rule that you will often follow when defining keyboard macros: try to make them executable N times in a row.
Actually, although it’s good to learn this general rule, there is a shortcut for just applying the last-defined keyboard macro to each of the lines of a region: 
  • ‘M-x apply-macro-to-region-lines’
Or to be more precise (from the documentation of the command):
  For each complete line between point and mark, move to the beginning
  of the line, and run the last keyboard macro.
You can name the last-defined keyboard macro, so that you can then execute it by name: 
  • ‘M-x name-last-kbd-macro’ – Name the last-defined keyboard macro.
Why do that? One reason is that it lets you have more than one macro defined at the same time. Once you have named this macro, say, ‘my-macro’, you can define another one that does something else. Then, you can execute the second macro using ‘C-x e’ and execute ‘my-macro’ as if it were a standard Emacs command: ‘M-x my-macro’. If you also name the second macro, then you can define a third one…
Keyboard macros are only defined for your current Emacs session. If you want to be able to reuse a macro in a future session, then save it in your initialization file (.emacs or _emacs). Open the file, then insert the macro definition using this command: 
  • ‘M-x insert-kbd-macro’ – Insert a named keyboard macro into your initialization file.
Now you can use ‘M-x’ to execute the named keyboard macro in future sessions – just as if it were a standard Emacs command of the same name. If you want to bind it to a key, you can add code like the following to your initialization file:
    (global-set-key (kbd "C-c a") 'my-macro)
This binds the key sequence ‘C-c a’ to the macro ‘my-macro’.
If you want to edit the macro in future sessions, you need to let the kmacro library know that the symbol is indeed a macro by putting the kmacro property on the symbol.
  (put 'my-macro 'kmacro t)

No comments:

Post a Comment