Emacs: Repeat Last Command

By Xah Lee. Date: . Last updated: .

here we introduce 2 very useful commands to increase emacs use efficiency. repeat-complex-command and repeat .

M-x repeat-complex-command

One command i use few times a day is repeat-complex-command. This command is great for:

For example, Alt+x query-replace and type the search text and replacement text. [see Emacs: Find Replace in Current File]

Now, to do that again, you can Alt+x repeat-complex-commandCtrl+x Alt+:】. Now you don't have to type the search/replacement text again.

But, the extra beauty here is that it shows the elisp code that's actually called. In this case, it's this

(query-replace "aa" "bb" nil (if (use-region-p) (region-beginning)) (if (use-region-p) (region-end)) nil nil)

(it might be slightly different on your emacs setup)

This is very useful because:

M-x list-command-history

Alt+x list-command-history is a very useful command. It list all recent commands called by Alt+x.

M-x repeat

Another useful command is Alt+x repeatCtrl+x z

It repeat most recently executed command.

What makes it very useful is that, once you called repeat, you can call it again by pressing or holding down the last key used for invoking repeat.

That means, for any command that does not have a shortcut (or if the shortcut is difficult to press), now you can repeat it by simply holding down a key.

For example, try Alt+x forward-word, now Ctrl+x z z z. It'll do forward-word 4 times.

This is especially useful to run last keyboard macro call-last-kbd-macro, which does not have a shortcut key. [see Emacs: Keyboard Macro]

Major mode commands usually have keys such as Ctrl+c Ctrl+key that is hard to repeat. So the repeat command is super useful.

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys

ELisp

ELisp Examples

ELisp Write Major Mode


Emacs Tutorial

Quick Start

Manage Windows

File

Buffer

Find Replace

Copy/Paste

Unicode

Whitespace

Rectangle Edit

Line Wrap

Shell

View Special File

Working with Brackets

Org Mode

HTML

Emacs Efficiency

Misc