Is there a (repeat-last-command) in Emacs?

EmacsElisp

Emacs Problem Overview


Frequently, I've dug into apropos and docs looking for something like the following only to give up to get back to the task at hand:

(repeat-last-command)

do the last C- or M- command I just executed (to be rebound to a fn key)

or sometimes the related:

(describe-last-function)

what keystroke did I just mistakenly issue, the effect of which I'd like to add to my bag of tricks. describe-key is close, but requires knowing what I typed.

Am I simply asking too much from my trusty sidekick?

Emacs Solutions


Solution 1 - Emacs

Repeat functionality is provided by the repeat.el Emacs Lisp package, which is included with standard Emacs distributions. From repeat.el's documentation:

> This package defines a command that > repeats the preceding command, > whatever that was, including its > arguments, whatever they were. This > command is connected to the key C-x z. > To repeat the previous command once, > type C-x z. To repeat it a second time > immediately after, type just z. By > typing z again and again, you can > repeat the command over and over.

To see additional information about the repeat command, type C-h F repeat RET from within Emacs.

Solution 2 - Emacs

Repeat last command

C-xz

Once you pressed it, just press only z after that and it will repeat (without having to press C-x again).

Solution 3 - Emacs

Yes, there is a repeat command. It's called repeat:

  • You can repeat commands with C-x z, and hit z to keep repeating.

A bit shocking nobody mentioned repeat-complex-command, available from the key binding C-x ESC ESC.

Solution 4 - Emacs

with regards to 'describe-last-function':

There's a variable last-command which is set to a symbol representative of the last thing you did. So this elisp snippet - (describe-function last-command) - ought to bring up the documentation for the thing that immediately happened.

So you could make a trivial working describe-last-function like so

(defun describe-last-function() 
  (interactive) 
  (describe-function last-command))

Put that elisp in .emacs or equivalent, and you'll have a M-x describe-last-function.

If you've banged on a few keys or done something that modified last-command since the thing you're interested in, the command-history function might be of interest. You can get that by M-x command-history

Solution 5 - Emacs

Also, M-x view-lossage shows you the last hundred(?) keystrokes you entered. So, you'll be able to see where the command is. It's what i used until i just right now found out about M-x command-history which i think i'll be using with C-h w now.

Solution 6 - Emacs

I'm not really sure, but maybe you are searching for this one?

> The command C-xz (repeat) provides another way to repeat an Emacs command many times. This command repeats the previous Emacs command, whatever that was. Repeating a command uses the same arguments that were used before; it does not read new arguments each time.

Emacs Manual, 8.11 Repeating a Command

Solution 7 - Emacs

May be this would help too... From emacs Help verbatim:

C-x M-ESC runs the command repeat-complex-command
  which is an interactive compiled Lisp function in `simple.el'.
It is bound to <again>, <redo>, C-x M-:, C-x M-ESC.
(repeat-complex-command ARG)

Edit and re-evaluate last complex command, or ARGth from last.
A complex command is one which used the minibuffer.
The command is placed in the minibuffer as a Lisp form for editing.
The result is executed, repeating the command as changed.
If the command has been changed or is not the most recent previous command
it is added to the front of the command history.
You can use the minibuffer history commands M-n and M-p
to get different commands to edit and resubmit.

Solution 8 - Emacs

Personally I found Sebastian's idea useful. Here is a working version

(global-set-key "\C-r" #'(lambda () (interactive)
				                 (eval (car command-history))))

Solution 9 - Emacs

This is old, but Google pops post this up first when I was looking to retrieve the last command I typed at the Emacs prompt. None of these answers worked for me so I decided to put in my two cents for those who might stumble upon this later on as I did. I'm using Portacle, but I found what I was looking for in here so I'm hoping it's generic enough to work with different setups. Anyway, what worked for me is using C-↑ and C-↓ to cycle through the history. Using M-p and M-n worked as well, but I prefer using the arrows since I use Bash quite a bit.

Solution 10 - Emacs

dot-mode is a way to repeat the last command(s).

From its commentary: > It emulates the vi `redo' command, repeating the > immediately preceding sequence of commands. This is done by > recording input commands which change the buffer, i.e. not motion > commands.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionackView Question on Stackoverflow
Solution 1 - EmacsEmerick RogulView Answer on Stackoverflow
Solution 2 - EmacsJohannes Schaub - litbView Answer on Stackoverflow
Solution 3 - EmacsashawleyView Answer on Stackoverflow
Solution 4 - EmacscmsView Answer on Stackoverflow
Solution 5 - EmacsquodlibetorView Answer on Stackoverflow
Solution 6 - EmacsechoxView Answer on Stackoverflow
Solution 7 - EmacsMurali VPView Answer on Stackoverflow
Solution 8 - EmacssabofView Answer on Stackoverflow
Solution 9 - EmacsDoug FView Answer on Stackoverflow
Solution 10 - EmacsMicah ElliottView Answer on Stackoverflow