0

After running something with M-x some-command, how do folks get back to the same command?

In this example (assume C-x w h is not available please) I try to match some text between DATABASE_URL=wildcard<space>, and after learning it fails, I go back to revising the regex by doing

M-x
hi space regexp

Note that's ten key presses. I'm guessing theres a way to do it with 2 or 3 presses. Can I simply tell emacs to simply "invoke my most recently invoked M-x command"?

NOTE: I do not know beforehand when I'm running a M-x command whether I would need to re run it. So, macro recording is not suitable for this use case.

enter image description here

american-ninja-warrior
  • 3,773
  • 2
  • 21
  • 40
  • 1
    A subsequent search turned up duplicate question [How to repeat a command immediately after calling it?](https://emacs.stackexchange.com/questions/24526/how-to-repeat-a-command-immediately-after-calling-it). – phils Feb 21 '18 at 00:59
  • [Repeating any last command (complex or not)](https://emacs.stackexchange.com/questions/13100) may also be of interest. – phils Feb 21 '18 at 01:00
  • american-ninja-warrior, 55 of your questions have at least one answer, and you have accepted a mere 10 of them. I have not reviewed the Q&As, but I would suggest that this proportion is much lower than it ought to be. Please take a moment to read https://emacs.stackexchange.com/help/someone-answers (and make sure that you are doing likewise on any other stackexchange site you use). – phils Mar 23 '18 at 01:48
  • If you're using Ivy, `ivy-resume` may help too. – Asme Just Apr 04 '20 at 01:42

2 Answers2

2

C-xz calls repeat which repeats the most recently-excuted command.

Note especially that you can keep repeating the command with only the final key in the sequence. e.g. C-xzzzzz

C-x z runs the command repeat (found in global-map), which is an
interactive autoloaded compiled Lisp function in ‘repeat.el’.

It is bound to C-x z.

(repeat REPEAT-ARG)

Repeat most recently executed command.
If REPEAT-ARG is non-nil (interactively, with a prefix argument),
supply a prefix argument to that command.  Otherwise, give the
command the same prefix argument it was given before, if any.

If this command is invoked by a multi-character key sequence, it
can then be repeated by repeating the final character of that
sequence.  This behavior can be modified by the global variable
‘repeat-on-final-keystroke’.

‘repeat’ ignores commands bound to input events.  Hence the term
"most recently executed command" shall be read as "most
recently executed command not bound to an input event".

To quickly access a recent command via M-x just type M-p or <up> to move back through the command history.


If you want to repeat a command including the interactive arguments that you entered, you can use C-xM-: to call repeat-complex-command (and once again the history is available).

repeat-complex-command 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.
phils
  • 48,657
  • 3
  • 76
  • 115
  • 2
    (And instead of `C-x M-:` you can use `C-x ESC ESC`. For some reason, I find that quicker/easier. YMMV.) – Drew Feb 21 '18 at 02:05
1

You can go through your past choices in the M-x prompt by typing M-p and M-n. This way, repeating your last M-x boils down to M-x M-p RET and you can re-answer all prompts you want (where these keys should work as well).