5

I often press the wrong combination of keys and open some obscure minibuffer mode. I have to press ESC three times (I think), which is pretty annoying. How do I make emacs quit the minibuffer with just one press of ESC?

3
  • 2
    C-g should default to abort-recursive-edit which will exit the minibuffer, unless you have another recursive-edit active. You could also write your own function using top-level which aborts all levels of recursive-edit and exits the minibuffer. I like a custom function written by Stefan which lets me control the escape key to use it as both a modifier key and also as a key that works with just one press: stackoverflow.com/questions/20026083/…
    – lawlist
    Aug 12, 2014 at 7:26
  • Can this be moved to SO so that I can place a bounty on the question and get some usable elisp? Aug 13, 2014 at 3:53
  • Here is something like what you are seeking, but it is not recommended. You can always comment it out later if you decide you want the default behavior: (global-set-key (kbd "<escape>") 'top-level) (define-key minibuffer-local-map "<escape>" 'top-level) (define-key minibuffer-local-ns-map "<escape>" 'top-level) (define-key minibuffer-local-completion-map "<escape>" 'top-level) (define-key minibuffer-local-must-match-map "<escape>" 'top-level) (define-key minibuffer-local-isearch-map "<escape>" 'top-level) My recommendation, however, is to use the function written by Stefan (above).
    – lawlist
    Aug 13, 2014 at 4:04

5 Answers 5

3

Thanks for all your ideas. Seems like this should do the trick:

(define-key minibuffer-local-map (kbd "ESC") 'keyboard-escape-quit)

It only worked for isearch. Seems like I'd have to rebind it for every single function (smex, ibuffer, and all those other that uses the minibuffer).

2

I believe that ergoemacs uses the ESC key the way you want. It also changes lots of other Emacs key bindings - but probably in ways that agree with your desire to "avoid long emacsy keypresses".

[Caveat: I am no expert on ergoemacs, and I do not recommend using ESC that way. ESC has a particular role in Emacs wrt the Meta modifier and keymaps, and treating it otherwise is asking for trouble sooner or later. (Similarly, C-g is fundamental to Emacs, and is even hard-coded in some cases, so it is not 100% replaceable by another key.)]

1

I usually use the following key combination: Ctrl-X (enter command-mode) K (Kill), then Enter.

This should kill the current mini-buffer.

1
  • Thanks, but I am looking to avoid long emacsy keypresses. That is one more keypress than I currently use (ESC ESC ESC); I'd just like to press ESC once to exit the minibuffer. Aug 12, 2014 at 9:20
0

I wanna do similar thing: press ESC to cancel the save-buffers-kill-terminal function during the "Save file" prompt.

I tried to rebind ESC in many minibuffer keymaps as mentioned in this Q&A. However, I still cannot cancel the "Save file" prompt by ESC.

After digging some elisp source codes, I found that query-replace-map is the keymap-parent of map-y-or-n-p, which is used by save-buffers-kill-terminal. So my final fix is:

(define-key query-replace-map (kbd "<escape>") 'keyboard-quit)

Note that it disables all shortcuts with meta key.

0

What you probably want is to use minibuffer-setup-hook to bind ESC in every minibuffer's local map (that way, your "ESC" keybind should usually win against whatever the current function or mode is doing, even if it's using a different key map).

    (add-hook 'minibuffer-setup-hook (lambda ()
        (local-set-key (kbd "ESC") 'abort-minibuffers))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.