The Mastering Emacs ebook cover art

Emacs 28 Edition is out now!

Read a Free Sample

Learn More

Repeating Commands in Emacs

How to repeat the last executed command in Emacs and how to convert the last executed command in Emacs to a complex command you can store for later use.
Updated for emacs 28

Repeating a command you just carried out is useful thing to know about, and yet because it’s bound to C-x z it’s unknown to most.

Like the . command in vi, the M-x repeat command will repeat the last action, but it does ignore input events, like moving around.

To save you from press the rather awkward keybind every time you want to repeat something, you can repeatedly press z after your first invocation to call repeat. Of course, you can also use the universal argument to repeat the command N number of times.

Although the repeat feature is useful, it’s best used for simple things. If you want to do complex chains of tasks, you should read why I think keyboard macros are misunderstood. It’s a deep dive into keyboard macros in Emacs and why they are so powerful. And no, they’re not just for munging text!

In a similar vein, you can exploit Emacs’s ability to turn any command you just executed into elisp. That elisp you can then invoke at will by evaluting it any way you see fit. You can also create a key binding for it.

The choices in Emacs are endless.

Further Reading

Have you read my Reading Guide yet? It's a curated guide to most of my articles and I guarantee you'll learn something whether you're a beginner or an expert. And why not check out my book?

Subscribe to the Mastering Emacs newsletter

I write infrequently, so go on — sign up and receive an e-mail when I write new articles