Emacs: Repeat Last Command
Here are 2 useful commands to increase emacs use efficiency.
repeat-complex-command
and
repeat
.
Command: repeat-complex-command
One command i use few times a day is
repeat-complex-command
. This command is great for:
- Repeat the last command that has prompts.
- Get the actual emacs lisp code to do the same thing, with filled arguments.
For example, Alt+x query-replace
and type the search text and replacement text.
[see Emacs: Find Replace in Current File]
Now, to do that again, you can Alt+x repeat-complex-command
【Ctrl+x Alt+:】. Now you don't have to type the search/replacement text again.
But, the extra beauty here is that it shows the elisp code that's actually called. In this case, it's this
(query-replace "aa" "bb" nil (if (use-region-p) (region-beginning)) (if (use-region-p) (region-end)) nil nil)
(it might be slightly different on your emacs setup)
This is very useful because:
- You can copy the lisp code, paste it in a buffer. Then you can run the lisp code by
eval-last-sexp
. [see Emacs: Evaluate Elisp Code] You can also duplicate the lisp code, and easily change the parameters and run it again. - You can use it to translate regex string from interactive form use to the elisp string version, with all proper double backslash escape added. [see Emacs: Regular Expression]
Command: list-command-history
Alt+x list-command-history
is a very useful command. It list all recent commands called by Alt+x.
Command: repeat
Another useful command is
Alt+x repeat
【Ctrl+x z】
It repeat most recently executed command.
What makes it very useful is that, 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 forward-word
4 times.
This is especially useful to run last keyboard macro
call-last-kbd-macro
, which does not have a shortcut key.
[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.