Emacs: Repeat Last Command
here we introduce 2 very useful commands to increase emacs use efficiency.
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 regex tutorial]
is a very useful command. It list all recent commands called by Alt+x.
Another useful command is
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
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.
Ctrl+x z z z.
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
that is hard to repeat.
repeat command is super useful.