Executing a Named Macro

Once you have given a macro a name, you can execute it by typing the command ESC x macroname RETURN. Emacs executes the macro. You can repeat the macro n times by typing ESC n ESC x macroname RETURN. You can have any number of named macros in use; you aren’t limited to one, as you are with keyboard macros.

If you save the macro in your .emacs file, it’s easy to execute; it will always be available whenever you’re running Emacs. Simply type ESC x macroname RETURN.

If you save the macro in some other file, it won’t be loaded automatically. Before executing the macro, you need to make Emacs read your macro definition file by using the command ESC x load-file. For example, let’s say that you have defined a macro called bold-word and placed it in the file nroff.macs, in the directory ~/emacs (the emacs subdirectory of your home directory). To make this macro available, give the command ESC x load-file ~/emacs/nroff.macs RETURN. You can load this file automatically by adding this line to your .emacs file:

(load-file "~/emacs/nroff.macs")

Once you’ve loaded the file, either manually or automatically through the .emacs file, you can give the command ESC x bold-word RETURN to your heart’s content.

Example

Here’s a trick you might find useful. Assume that you’ve saved a macro called make-double-spaced-text. Of course, typing ESC x make-double-spaced-text RETURN whenever you want to use the macro is time-consuming and, frankly, more work than adding carriage returns by hand. ...

Get Learning GNU Emacs, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.