<p>I don’t know about you, but from time to time I definitely have the need to re-run some Emacs command that takes user input (e.g. <code class="language-plaintext highlighter-rouge">query-replace</code>). For the purpose of this article let’s call such commands “complex” commands. Obviously we can just invoke the same command, provide again the input (directly or using some parameter history if available), but it would be nice if there was a faster way to do this.</p> <p>As it’s usually the case with Emacs, such a way already exists - the built-in command <code class="language-plaintext highlighter-rouge">repeat-complex-command</code>. By default it’s bound to both <code class="language-plaintext highlighter-rouge">C-x M-:</code> and <code class="language-plaintext highlighter-rouge">C-x M-ESC</code>.<sup id="fnref:1"><a class="footnote" href="https://emacsredux.com/blog/2021/11/24/redo-complex-command/#fn:1" rel="footnote">1</a></sup> When you invoke the command you’ll see in the minibuffer the last Emacs command you ran and you can use <code class="language-plaintext highlighter-rouge">M-n</code> and <code class="language-plaintext highlighter-rouge">M-p</code> to move forward and backward in the command history.</p> <p>You’ll notice that the commands are listed as Emacs Lisp code that you can edit directly if needed. You’ll also notice there are no commands that don’t any parameters on the list. Imagine something like:</p> <div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">describe-key</span> <span class="o">'</span><span class="p">((</span><span class="s">"\272"</span> <span class="o">.</span> <span class="nv">[134217786]</span><span class="p">)))</span> <span class="p">(</span><span class="nv">occur</span> <span class="s">"emacs"</span> <span class="no">nil</span><span class="p">)</span> <span class="p">(</span><span class="nv">projectile-ag</span> <span class="s">"emacs"</span> <span class="no">nil</span><span class="p">)</span> <span class="p">(</span><span class="nv">markdown-insert-gfm-code-block</span> <span class="s">"emacs-lisp"</span> <span class="no">nil</span><span class="p">)</span> </code></pre></div></div> <p>This might also give you a hint as to why the keybinding <code class="language-plaintext highlighter-rouge">C-x M-:</code> was chosen - it’s pretty close to <code class="language-plaintext highlighter-rouge">M-:</code> (<code class="language-plaintext highlighter-rouge">eval-expression</code>), which allows you to run arbitrary Emacs Lisp code from the minibuffer.</p> <p>When it comes to Emacs we’re all constantly learning. I didn’t know about this command until several days ago I saw it in a <a href="https://twitter.com/mickeynp/status/1457826371057639429?s=20">tweet</a> shared by <a href="https://twitter.com/mickeynp">Mickey Petersen</a>, a true Master of Emacs. Thanks for the tip, Mickey!</p> <div class="footnotes"> <ol> <li id="fn:1"> <p>Not exactly the most convenient keybindings, right? <a class="reversefootnote" href="https://emacsredux.com/blog/2021/11/24/redo-complex-command/#fnref:1">↩</a></p> </li> </ol> </div>