4

So I have finally switched to Emacs, not so much because I'm yet convinced it is in itself the better editor but because it certainly does have more powerful extensions. I am still using vim-mode though, perhaps that's part of my problem... but I really don't intend to abandon the modes-approach, so I'll probably stay with it.

I'm getting along quite well, but one thing I find really unnerving is the behaviour of the esc key (which I have in the shift-lock position). I'm used to relying on this a lot as more or less a "panic key", which may not be nice but I find allows me to work generally quite a bit less caring about the keystrokes themselves, and thus faster.

What I'd like this key to do is just get me out of any minibuffer or special editing mode into a well-defined normal state. Perhaps most importantly, I would like it to not do anything unrelated,

  • Simulate meta. What do I have an alt key for?
  • Close windows I'm not even in at the time.
  • Getting interpreted as the final key in some key sequence.
  • ...

Is it possible to turn all that off and make esc an actual escape key? Vim-mode does make it behave kind of as I like in some situations, but especially when other plugins are involved this often breaks.

Alternatively, are there different options that might suit my kind of workflow?

  • 1
    @Memming: I don't mean "abort key", I don't mean "close that there key", I don't mean "do that thing key". I mean "get me out of here with as little mess as possible key". Out being standard editing mode, where everything behaves as I'm used to. In vim, ESC does quite a good job in getting me always to Normal Mode when possible. In Emacs, this often fails on account of it interpreting the key in some other way that I don't expect. – leftaroundabout Nov 6 '13 at 17:26
  • I see. Sorry, didn't understand your question properly. Now I get it. – Memming Nov 6 '13 at 17:29
  • In Emacs, the Esc key does "something" -- so you need to decide which functions and situations you want it to do something, and disable it for all other situations. – lawlist Nov 6 '13 at 19:23
  • Perhaps you could remap your caps lock key to C-g or ESC ESC ESC (the latter does exit from most things in Emacs). But then you'd need to use something else for Vi-style normal mode, like C-c (if that's supported by vim-mode). – echristopherson Nov 7 '13 at 21:28
  • I put (global-set-key (kbd "M-C-g") 'vim:activate-normal-mode) in my .emacs and stuff works pretty satisfyingly now, albeit with a lot of the "boilerplate-ish" C-g ESC combination. – leftaroundabout Dec 10 '13 at 17:23
1

The behavior for which you're looking is embodied in the Emacs Lisp function keyboard-quit, bound by default to C-g (Control-g). Its behavior is roughly equivalent, in the context of Emacs, to the usual function of C-c in a shell; it will interrupt whatever Lisp code is running, and drop you back to the top level. (In particular, if you're being prompted for text entry in the minibuffer and decide you don't want to do whatever it is that's prompting you after all, C-g is how you cancel it.)

A single press of the Escape key, by default, acts as a Meta prefix key; that is, a keybinding which involves the Meta key (e.g. M-x, execute-extended-command) will work just as well if you press Alt-x, or if you press Escape, then press X -- Emacs doesn't differentiate. Triple-pressing the Escape key will run keyboard-escape-quit, which is like keyboard-quit but with more of a "do what I mean" behavior -- something which I've never needed in several years of using Emacs, and indeed didn't know about until a few minutes ago, but you may find it useful.

Speaking of things you may find useful: You can obtain documentation on any Emacs function via the describe-function function, invoked as C-h f FUNCTION-NAME RET (that is, press and release Control-h, press f, type in a function name at the prompt, press Return), or as M-x describe-function RET FUNCTION-NAME RET. For variables, it's the same, except C-h v or describe-variable, plus the variable name; for keystrokes, it's C-h k or describe-key, and you type the key chord whose binding you want described. These three functions, plus C-h m to retrieve documentation on the current buffer's major and minor modes, are enormously useful in discovering how Emacs works and what it can do, and I enthusiastically recommend them to you as the best and quickest way to reduce the degree to which Emacs seems alien and intimidating.

Now, finally, to answer your actual question: It may be possible to reconfigure Emacs' default key mapping, such that it differentiates the Escape key and the Meta key. However, I don't really recommend you try to do so, for a couple of reasons.

First, while I'm sure there is a way to do so, it is necessarily going to be obscure and tricky, and it will have a lot of potential to go wrong in non-obvious ways -- something which, while fine for Emacs veterans, isn't really advisable when you're trying to learn the editor. Worse, if you try to do it in the naive and obvious fashion (e.g. M-: (global-set-key (kbd "ESC") 'keyboard-quit) RET), you'll break Meta key behavior entirely. Actually getting it to work right will require descending into the lowest levels of Emacs' input event processing -- wear a headlamp, bring a machete, and pack a lunch. (Maybe two lunches.)

Second, even if you can get that binding working reliably, it's very much non-standard, whereas any Emacs you use, anywhere, will certainly have C-g bound to keyboard-quit. Especially while learning the editor, it's advisable to get as familiar as you can with the default keybindings, instead of rebinding things in a fashion which will only work with your own Emacs init files. (If you can guarantee either that you'll never use an Emacs anywhere but on your own box, or that you'll always be able to install your init files, great! Go for it. Also, for this purpose, a Vim mode which is part of the Emacs standard library counts as "default", because you can invoke it on any properly installed Emacs via M-x vim-mode RET or similar.)

tl;dr: You can probably do that with ESC, but it'll take horrible hackery to do so, assuming it's possible at all, and you're better off getting used to the facilities Emacs provides by default for invoking the behavior you want.

|improve this answer|||||
  • Thanks for your answer. I start to get used to C-g. Unfortunately, vim-mode does require me to actually press ESC to get into its Normal Mode, so I now often spend three keystrokes when one should suffice (and if I get the order wrong, Emacs complains about M-C-g not being defined and I'm out of my flow again). Would you have any idea what could yet be done better? – leftaroundabout Nov 24 '13 at 17:57
  • @leftaroundabout You might be able to bind M-C-g to perform both actions; it'd involve doing C-h k RET ESC RET to see what's bound to ESC in Vim insert mode (let's assume it's a function called vim-normal-mode, and then binding M-C-g to e.g. (lambda () (keyboard-quit) (vim-normal-mode)). – Aaron Miller Nov 25 '13 at 16:15
4

You can use this in your Emacs init file:

;;; esc always quits
(define-key minibuffer-local-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-ns-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-completion-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-must-match-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-isearch-map [escape] 'minibuffer-keyboard-quit)
(global-set-key [escape] 'keyboard-quit)
|improve this answer|||||
  • Lovely! Changed the last line to (global-set-key [escape] 'keyboard-escape-quit) instead, to truly act as a three-escape-mashes – Ole Aug 17 '16 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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