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

1

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.
  • 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 2:05
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.