To save a keyboard macro for longer than until you define the
next one, you must give it a name using M-x name-last-kbd-macro.
This reads a name as an argument using the minibuffer and defines that name
to execute the macro. The macro name is a Lisp symbol, and defining it in
this way makes it a valid command name for calling with M-x or for
binding a key to with
global-set-key (see Keymaps). If you
specify a name that has a prior definition other than another keyboard
macro, Emacs prints an error message and nothing is changed.
Once a macro has a command name, you can save its definition in a file. You can then use it in another editing session. First visit the file you want to save the definition in. Then use the command:
M-x insert-kbd-macro RET macroname RET
This inserts some Lisp code that, when executed later, will define the same
macro with the same definition it has now. You need not understand Lisp
code to do this, because
insert-kbd-macro writes the Lisp code for you.
Then save the file. You can load the file with
(see Lisp Libraries). If the file you save in is your initialization file
(see Init File), then the macro will be defined each
time you run Emacs.
If you give
insert-kbd-macro a prefix argument, it creates
additional Lisp code to record the keys (if any) that you have bound to the
keyboard macro, so that the macro is reassigned the same keys when you
load the file.