ChatGPT解决这个技术问题 Extra ChatGPT

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

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?

C-x z (repeat) and C-x M-z (repeat-complex-command). See also (repeat-matching-complex-command).

E
Emerick Rogul

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.


Z
Zach Young

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).


Exactly what I expected it to do.
a
ashawley

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.


similar to M-x M-p except that repeat-complex-command repeats previous arguments.
Also on C-x M-: (mnemonic: like M-: but with the elisp expression already filled in for you).
Yes, shocking indeed. The accepted answer is not the best answer here, IMO (but the OP apparently feels it corresponds best to her question). To me, this answer is closest to what the written question asked for (repeat last command). And the possibilities offered by repeat.el (which subsumes C-x z z z...) are the second closest. Wrt the second question asked, about which keystrokes were hit last, the best answer is C-h l (view-lossage). There is little use, IMHO, for a describe-last-command command.
You can also use C-x M-: as part of a keyboard macro, which can be handy. The default repeat-complex-command bindings are a little too cumbersome to type over and over.
C
Chris Conway

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


q
quodlibetor

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.


p
phils

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


and as noted by Johannes Schaub, subsequent presses of just z on its own will continue the repetition.
M
Murali VP

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.

s
sabof

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

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

Considering that isearch and isearch-backward are important navigation commands, and that the latter is bound to <kbd>C-r</kbd> by default, one could probably find a better binding for repeat-command. On a Mac, I'd suggest Command(super)-Y, since some apps already have a convention of using that as the opposite of 'undo'.
D
Doug F

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.


M
Micah Elliott

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.