Emacs Keyboard Macros To Speed Up Your Workflow

Emacs keyboard macros for beginners

The ideal Emacs user is always looking for ways to speed up their work, or, as the saying goes, “Work smarter rather than harder.” Defining Emacs keyboard Macros will not only help you save time in your text editing but can also help you get a handle on some programming fundamentals.

You can use Emacs locally but also install a terminal version for your VPS hosting. Emacs Tramp also lets you access your servers from within Emacs locally. The latter is considered more beneficial since you can carry your Emacs keyboard macros to any server you visit.

Emacs Keyboard Macros Save Time

Be sure to check out the official Emacs macro documentation.

Repeating your work can be a tedious and laborious process, particularly when you work with a myriad of different text files that can often be formatted in different ways. Emacs keyboard macros help you save time by giving you the ability to easily replicate your commands as many times as you need to get a job done.

Some Use Cases For Macros

A keyboard macro is a step below a function. This means if you have a small issue that would not warrant the writing of a whole function, then the macro is the best choice. Writing a function is better suited to a process that is more general and repeatable across a wider array of circumstances. For something specific (and quick), needed only when you need it and never again, a macro fits nicely. (However, if you have a particularly helpful macro that you will need to use again, you can save it and bind to a key.)

Just in case you’re wondering whether or not a keyboard macro is the best way to complete a certain job, here are a few idea use cases in which macro would work great:

  • You want to delete a repeated sequence of text objects in a file but a normal search and replace is not possible.
  • You have a specific set of commands that you must complete in a file, and you would like to execute them repeatedly until instances are covered.

Practice Some Programming Fundamentals

Practicing macro definitions can also help solidify some programming concepts. Defining a macro is basically a quick way to record instructions that the Emacs Lisp interpreter can understand. In essence, this is the one of the ways that you define functions in the Emacs Lisp programming language. The practical applications of macros can help you see first hand how to give direct instructions to Emacs in the language that it understands. This will help you get a jump start when you eventually proceed onto writing your function definitions.

How to Start Using Macros

For every macro, there are two stages. You must first define the macro and then run the macro as many times as you need it. Both of these stages are detailed below.

Defining a Macro

In order to start recording Emacs keyboard macros you only have to press the <f3> key on your keyboard. The next series of keyboard input commands you use will be recorded in memory. When you have finished the repeatable sequence of keys, press the <f4> key.

In case you have re-mapped the <f3> <f4> keys to some other function, these are the default macro functions that are being given to Emacs:

<f3> runs the command kmacro-start-macro-or-insert-counter (found in
global-map), which is an interactive compiled Lisp function.

<f4> runs the command kmacro-end-or-call-macro (found in global-map),
which is an interactive compiled Lisp function.

Running a Macro

In order to run your macro, you can press the <f4> key again and repeat that entry until you have completed all the work that the macro is able to do. This is the easiest way to run the macro you previously recorded with <f3>. Below, you’ll see a few different ways for interacting with your macros. But, for the most basic usage, <f3> and <f4> has got you covered.

Additional Macro Options

Below you will see a few different ways for interacting with one macro or different macros saved in a ring.

The Keyboard Macro Ring

If you have to save multiple macros for use in different documents, you can save a current macro to the “macro ring.” The macro ring is just what it sounds like: a ring that holds multiple values and lets you rotate through its contents.

All keyboard macro commands begin with the prefix C-x C-k. This makes it easier to remember the various iterations and their key bindings. C-x C-k C-k executes the current macro at the head of the ring. C-x C-k C-n cycles to the next macro in the ring, and C-x C-k C-p goes to the next macro in the ring. Notice, after the C-x C-k prefix, you have k (the current macro) n (the next macro) and p (the previous macro). You’ll find that within many Emacs functions and modes, various commands for next and previous will be mapped to n and p keys. Unless you map different commands to these keys, you’ll find the rule is fairly reliable.

The Universal Argument Command

Emacs provides a “universal-argument” command, which can be used to pass a numerical argument to another command. Basically, when you run the universal argument command, you are prompted to provide a numerical argument that will passed on to the next command.

In essence, the universal argument allows to repeat a command as many times as you have dictated. For example, if you start recording a macro, press the letter “l,” stop recording the macro, then you have a macro that will insert a letter “l” onto your line. If you prefix <f4> with a universal argument plus the number 5, for example, you will have five letter “l” characters inserted into your line.

So, if you know that you only need to repeat your macro for a definite number of iterations then you can prefix the <f4> with a universal argument. If not, you can can just keep smashing that <f4> key until all your work is done.

The default key binding for the universal argument is C-u. Here’s the entry from the Emacs help menu:

C-u runs the command universal-argument (found in global-map), which
is an interactive compiled Lisp function.

For more Emacs-related articles be sure to check out the Emacs education channel.

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? Let us know!