Save Keyboard Macros as Functions in Emacs

Save keyboard macros as functions in Emacs

There will be many instances in which a valuable sequence of commands may need to be repeated an indefinite amount of times. Instead of endlessly typing and re-typing the same set of commands, you can use keyboard macros in Emacs to record and save those key command sequences as functions that can be called upon later.

Installing and using Emacs is ideal for private hosting but can be used on cPanel VPS hosting as well.

Be sure to first check out our full guide on how to record and playback keyboard macros in Emacs.

The Basic Sequence

Here is the basic sequence of recording and saving macro definitions:

  1. Find a helpful sequence of keys
  2. Record the sequences with <f3>
  3. Play the sequence back with <f4>
  4. If everything works as expected, give the key sequence a name to save it as a function in memory
  5. Optionally, bind the macro (now a function of its own) to a key
  6. Insert the keyboard macro definition into a buffer as Lisp code so you can reload it later

Why Use Keyboard Macros?

Just about everything you do in Emacs involves a sequence of key inputs. Every time you press a key or combination of keys, different functions run in the background. This means you can save time and energy by taking key sequences you use frequently and saving them as a new custom function. Your custom function is basically just a series of key commands.

This is similar to shell scripting. However, in shell scripting you would use an interpreted language like bash, for example, to create a series of commands, loops, pipes, and other constructs to script a kind of shell function that does what you want it to do.

With Emacs, you can use the keyboard macros in a similar fashion, but you don’t have to know much about Lisp code in order to record your function and save it for use later.

How To Name and Save Keyboard Macros in Emacs

Now you have arrived at the important part, understanding how to name and save keyboard macros in Emacs. Bearing in mind, the naming must be done before the saving. Once you have defined a macro (with <f3>) and run it once or several times (with <f4>), and decided you want to save it, you’ll start by giving it a name.

How to Name a Keyboard Macro

To name a macro, Emacs will use a pre-defined function called kmacro-name-last-macro. This function, as the name suggests, operates on the last macro you defined. That means the most recent macro in memory, still stored there, will be named by this function. The function is also bound to C-x C-k n. (Notice, n for “name” as a mnemonic hint.)

After running the naming function you will be prompted to type in the name of your function in the minibuffer.

Pro tip: Emacs Lisp allows for the use of namespaces. This way, you can put in a custom namespace to make sure that your function will not interfere with any predefined function in Emacs. Namespaces precede function name with some text and a forward slash (/). For example, if you wanted to use a namespace of “abc”, your custom function would be named something like this: abc/my-function.

Optional Binding

Your function is now written into temporary memory. This is the best time to bind your custom function to a key. You can do that with the kmacro-bind-to-key function, which is also bound to C-x C-k b. Notice, “b” for “bind” as the mnemonic clue for remembering this function. When prompted in the minibuffer, you will be asked to input your desired key.

Remember, many of the best keys are already used in various keymaps throughout your Emacs system. So what can you do? If you already know an available key sequence, you can go ahead and put it here. If you are inadvertently going to overwrite an existing key sequence, Emacs will warn you.

Here you have a few options. You can temporarily bind your current macro to an existing set key, knowing that when you close Emacs and open it later the function and key mapping will have been wiped from memory.

Or, you can use the reserved macro key bindings. Numbers and letters prefixed with C-x C-k have been reserved just for macros. This means you can use numbers 0-9 and letters A-Z (lower and upper case) to store your function. For example, you can bind your function to C-x C-k 1, and in practice that keyboard input would Control-x Control-k followed by the number 1.

(You’ll see how to save that function and key binding into permanent memory in the next section.)

Loading Your Macros as Stored Functions

You have already seen how to record and playback key sequences by defining keyboard macros. And you have seen how to name and bind those key sequences with function definition. Everything you have accomplished so far is stored only in the current Emacs session. But if you close Emacs and open it back up again the previous stored settings will have been flushed from memory. This is because you created keyboard macros, and named them and bound them to keys, but you did not save your work anywhere permanent.

Storage Options

There are many different ways to store your macros for later use. Basically, you need to store them in a place where they will be run at Emacs startup. This way, before you start your work, the Emacs Lisp interpreter will have read your function definitions and stored them in memory for use during your Emacs session.

You place your keyboard macro into your preferred storage location using an insert function:

M-x insert-kbd-macro

This function inserts your keyboard macro as Lisp code. To insert the keyboard macro with the key binding you selected, you need use a prefix argument with C-u (the universal argument). That would look something like this:

C-u <RET> M-x insert-kbd-macro <RET> <macro name> <RET>

After running this function, you will see your macro definition (printed out as Lisp code) into whatever buffer you are currently visiting. To save yourself some time, first visit your preferred storage location file and then run this function.

But where to store your function?

The Emacs “Init” File

The easiest storage location would be your init file. This file’s location depends on how you have Emacs configured. Emacs will check several different locations for this file.

  • You may have your init file in your user home directory ~/.emacs. In this case, use that file.
  • If you have your init file in the /.emacs.d/init.el file then make sure to use that one instead.

These are the various locations of your Emacs configuration file. You likely know exactly which file to use, but if not you must check and see how your system is configured or ask for help.

Using a “load-file” Function

The load-file function reads a file for input. This means you can store your keyboard macros in any file you want and load them via a function in your init file.

(load-file FILE)

The load-file function accepts any file path as a string. For example, if you have your functions in a file called “macros” in your home directory, your load function might look like this:

(load-file "~/macros")

Now, when you create, name, and bind a new function, you can visit your macros file and use the insert function to place it there.

If you don’t need cPanel, don't pay for it. Only pay for what you need with our scalable Cloud VPS Hosting.

check markCentOS, Debian, or Ubuntu check markNo Bloatware check markSSH and Root Access

Learn more about the differences between cloud vs VPS hosting, and why it matters.

Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Join the conversation!