Back out of minibuffer

Dmitry Alexandrov 321942 at
Thu Oct 12 11:38:17 CEST 2017

>>> Is there a convenient way to make e.g. M-x followed by backspace exit
>>> the minibuffer -- similar to how colon (evil-ex) followed by backspace
>>> behaves? (I'm learning Emacs and Evil at the same time ...)
>> Try with C-g (CTRL+g)[1]. C-g is also considered a proper way to abort
>> almost any running/typed command.
> Thank you, I did discover C-g -- I also bound [escape] to the same thing (as follows) because of my existing
> habits:
>> (define-key minibuffer-local-must-match-map [escape] 'abort-recursive-edit)

No good.  ESC is handy sometimes as a sticky meta.  The best example is when you want to type ‘M-x keyboard-escape-quit’ — the exit command that is stronger than C-g (keyboard-quit).  Out of a box it’s on triple escape (ESC ESC ESC), you could type ‘M-ESC ESC’ instead, of course, but this is less convenient.

It is possible, by the way, to map it to double ESC without side-effects (almost), even globally:

| (define-key global-map (kbd "ESC ESC") #'keyboard-escape-quit)

> However I notice that I reflexively use backspace to exit the minibuffer -- rather than unlearn, I'd like to
> teach Emacs to obey ... Is there a convenient way?

No idea, whether this qualify as a convenient way, but I cannot see a problem:

--8<---------------cut here---------------start------------->8---
(defun minibuffer-delete-backward-char-or-keyboard-quit (n &optional killflag)
  (interactive "p\nP")
  (if (= (point) (minibuffer-prompt-end))
    (delete-backward-char n killflag)))

(define-key minibuffer-local-map
  (kbd "DEL") #'minibuffer-delete-backward-char-or-keyboard-quit)
--8<---------------cut here---------------end--------------->8---

This acts as quit whenever you are DEL’ing through the left boundary, not only when the minibuffer is empty, so e. g. ‘M-x M-p DEL’ is also quit.  If you don’t like this, change (point) to (point-max).

More information about the implementations-list mailing list