You will be more comfortable in an X11 Emacs, which can receive keyboard input and display text without going through encoding and decoding for the terminal. The main reason to use a text mode editor is to run it inside screen or tmux on a remote machine, but thanks to Tramp, it's usually easier to edit the remote file in your local Emacs. That being said, you can do what you want as long as your terminal emulator cooperates, which is the case with Xterm.
When you press a key or key combination in a terminal, it is transmitted to the application running in the terminal as a sequence of one or more characters. For example, when you press a, the application receives a
. When you press Enter, the application receives the character CR
(a.k.a. ^M
(pronounced “control-emm”), a.k.a. character number 13, a.k.a. \r
or \015
). Key combinations involving Alt are typically transmitted as the character ESC
(a.ka. ^[
a.k.a. \e
or \033
) followed by the sequence for the key or key combination without Alt. Function keys and other key combinations are transmitted as escape sequences beginning with \e[
or \eO
.
The escapes sequences are not fully standardized, and terminals typically ignore certain attributes for certain keys. For example, the hyper and super modifiers are usually ignored. You can see what your terminal sends for a key combination by pressing Ctrl+V followed by that key combination in a shell prompt, or C-q
or C-h c
followed by the key combination in Emacs.
Xterm lets you configure the escape sequences for each key through X resources. Most setups read resources from ~/.Xresources
when X starts, and you can load the file manually with xrdb -merge ~/.Xresources
.
XTerm.VT100.translations: #override \n\
Ctrl ~Shift ~Meta <Key> Return: string("\033[73;5~") \n\
Ctrl Shift ~Meta <Key> percent: string("\033[37;6~")
A common convention uses escape sequences of the form ESC [ number1 ; number2 ~
for function keys with modifiers. number1
indicates the function key (15
to 24
for F5 to F12 — for historical reasons, F1 through F4 have different escape sequences) and number2
indicates the modifier (2
for Shift, 5
for Ctrl, 3
for Meta, 8
for Ctrl+Meta, and add 1 for +Shift — no, it isn't very consistent).
You'll have to specify the Hyper and Super modifiers through their number, as Mod1
through Mod5
. Run xmodmap -pm
to see the mapping of keysyms to modifiers. For example, if Super_L
is bound to Mod5, then the following line (use this exact case) defines a binding for Super+Space:
~Ctrl ~Meta Mod5 <Key> space: string("\033[32;16~") \n\
Emacs translates escape sequences into its internal key representation through input-decode-map
or local-function-key-map
(or function-key-map
before Emacs 23).
(define-key local-function-key-map "\033[32;16~" [(super ?\ )])
This post is made of 80% recycled material from Are there any linux terminals which can handle all key combinations?