Emacs: Repeat Last Command

By Xah Lee. Date: . Last updated: .

Here are 2 useful commands to repeat last command.

Repeat Last Command

Alt+x repeatCtrl+x z

repeat the last command.

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 it 3 times.

This is especially useful to repeat call-last-kbd-macro 〔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.

Repeat Last Complex Command

Alt+x repeat-complex-commandCtrl+x Alt+:

Repeat the last command that prompted for user to answer questions but without prompting again.

💡 TIP: repeat-complex-command is great for:

  • Repeating a command without answering the prompts again.
  • Get the actual emacs lisp code. Copy paste it elsewhere, edit and eval.
  • Converting emacs Regular Expression from interactive form to lisp string form with all proper backslash escape added. 〔see Elisp: Convert Regex to Lisp Regex String

Example of repeat-complex-command

  1. Alt+x list-matching-lines and type the search text (e.g. cat). 〔see Emacs: Find Replace in Current File
  2. Alt+x repeat-complex-command. You get (list-matching-lines "cat" nil). Press enter to repeat it, or, copy the code and paste in another buffer. you can edit it, and eval it by eval-last-sexp 〔see Emacs: Evaluate Elisp Code

List Command History

Alt+x list-command-history
list all recent commands called involving the Minibuffer.
emacs 29 list-command-history 2023-09-08
emacs 29 list-command-history 2023-09-08

Emacs Principle