May I recommend… escaping the minibuffer

A few days ago, I answered a question by Charlie Holland on the Fediverse with a “trick” I use when working with GNU Emacs.

On a recommendation from Charlie himself, I wrote this article to give a more “structured” (more like “unbound” I guess) explanation of what I usually do. It's meant for the May edition of the Emacs Carnival, hence the title.

In my usual flow I make great use of the other-window command (by default bound to C-x o) to move around the windows I have open while I program or anyway write something. For the most part I jump from one buffer with a file to another buffer with a different file to compare something or maybe just copy code.

Because I primarily use Emacs to program it is common to execute some command to start some kind of process. For example, M-x compile to run some kind of build tool.

Often, before launching the command I do a double-take: is the code really ready for this? Is this file I just edited complete or would I have to wait the end of the building process to discover there's a fatal flaw? And after it took so long…

Trivially I could just keyboard-quit (C-g) out of the prompt and investigate, but this approach comes with a few annoyances: to begin with I have to open the prompt again when I'm done and this can be anything from M-x to a complex key chord defined by the major mode. Also, if the command to execute is something that needed to be typed, after quitting I have to type it again or scroll through command history.

Most of the time they don't really matter, but they are still annoyances I can do without. That's where the trick I introduced but never revealed comes in.

The majority of Emacs's prompts wait until you press <RET> before resuming operations and this gives me the chance to C-x o out of the minibuffer to another window, scroll the content of the buffer to see if everything's alright and then C-x o again inside the minibuffer to submit the answer to the prompt.

Once the cursor is out of the minibuffer you are not limited to certain operations: you can select a region of text and delete it, or copy it, or comment it out. Maybe one of the arguments to give to the command you are about to execute is very long and you have the man page opened on a side window, so you navigate there, copy the option and paste it on the command line. As long as you don't press <RET> while the minibuffer is active you are free to do anything.

There's a caveat though: out of the box when the minibuffer has a prompt you cannot start another prompt, which means you cannot open files, switch buffers, etc. Setting enable-recursive-minibuffers to t changes this behaviour, enabling prompts during prompts. Thanks to Omar AntolĂ­n for introducing this variable to me.

Once the minibuffer-escaping trick clicks, it's very liberating: you don't have to C-g out of queries just because you needed to cross-reference something before running the command; your cursor is not locked to anything or any place, especially when enable-recursive-minibuffers is t, so you can still use the full power of the editor. The moment I harnessed this power, it immediately became part of my normal workflow and it inconveniences me a lot when I meet a prompt that does not respect C-x o.

There are fundamentally two biggest “enemies” to the “C-x o flow”: prompts like map-y-or-n-p (used in some bulk operations like save-some-buffers) and transient menus of Magit fame.

The first category is mostly made of special prompts for specific situations. Emacs itself doesn't really have that many but they are still present and once you are inside one, you either answer it or quit it. Thankfully, these prompts usually take only one answer or have a special key to give the same answer to all items (e.g. ! to mean “always yes”).

The second category is becoming more and more common in third-party packages. Transient menus are certainly useful, grouping together commands in a sort of “cascading” interface, making for good discoverability and ease of use. But, these menu devours the minibuffer and if you notice something when you are three menus deep, the only way out is three times C-g. You can't leave a sequence “hanging” while you take care of urgent business: you either select one of the options listed in the menu until you reach the end or you quit from every layer of the sequence.

When you are used to the “C-x o flow” you feel like there are no obstacles to your work: you let your thoughts dictate your next action and your muscle memory executes it, without ever stopping to wonder if you can do that or how to do that. And then, one of the unescapable prompts appear and it's like there an indestructible infinitely tall and infinitely wide wall in front of you, and the only way through is by knocking at the gate and waiting until they let you pass. Bummer!