Unlike GUI-based apps, terminal-based apps (like almost all of those that you run inside Terminal windows) are limited in which key combinations they can sense. Back when terminals were real hardware (not just emulated by programs like Terminal and xterm), terminals like DEC's VT-100 had a Control key, but it only worked in combination with certain other keys. Since most terminal emulation programs emulate a VT-100 (and its successors, including the VT-102 and the VT-220), there is no way to actually send a Control-Shift-5 (or whatever C-% is on your keyboard) to a terminal-based program.
You will probably have better luck mapping query-replace-regexp
to a key (combination) that actually transmits something. The ‘F keys’ are (usually) mapped in a VT-220 style that the xterm-color
termcap/terminfo entries know how to parse (F1-F4/F5 may be in a VT-100 style though!). If needed, you can configure Terminal to send whatever codes your termcap/terminfo is expecting (at least I can in the 10.4 Terminal). You can assign codes to a bare F-key or an F-key with a single modifier (Shift, Control, or Option). Any modifiers would not be passed (through the terminal) to Emacs as such, but you could map some of those combinations to the terminal key codes for higher function keys (F14-F20) (unless the combination is taken by a system shortcut—“Full Keyboard Access”, Expose, and Spaces(?)).
(global-set-key (kbd "<f13>") 'query-replace-regexp) ; I have a full-sized keyboard with an F13 on it.
Note that F1-F4 may not work properly, depending on the configuration of Terminal and emacs. For example, on my system (10.4), Terminal is configured with “ESCOP” (the VT-100 key code) for F1, but emacs expects “ESC[11~” (an ‘old’ xterm sequence). The keys work fine in emacs if I reconfigure either Terminal (⌘I; Keyboard) or emacs (see below).
; Expect VT-100 key codes for F1-F4
(define-key function-key-map "\eOP" [f1])
(define-key function-key-map "\eOQ" [f2])
(define-key function-key-map "\eOR" [f3])
(define-key function-key-map "\eOS" [f4])
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.
Best Answer
Ubuntu uses GNOME and
gnome-terminal
. According to its documentation (in version 2.30), CtrlShiftUp and CtrlShiftDown are used for navigating the scrollback buffer, and there doesn't seem to be any way to turn this "feature" off (aside from modifying the source code ofvte
, the terminal library).To patch vte on Ubuntu 10.04:
Rebuild
vte
, the terminal emulation library:Install software required for compiling:
Download
vte
source code and apply the patch:Make a Debian package and install it:
Close all
gnome-terminal
processes.vte
update...Patch for 0.23:
Patch for 0.26: