15

I have been struggling to find a simple and direct question to the answer and therefore I have decided to ask here.

Q: Is there a way to use Super in Emacs launched in a Terminal in Linux?

P.S: I read a lot of information about xmodmap but I think the key is that the terminal emulator cannot represent the Super key. However, (kbd "s-SPC") returns something and I was thinking of using the translation facility of .Xdefaults/X. Am I on the right path? Given the result of kbd, what do I need to put as XTerm.VT100.translations?

0

5 Answers 5

22

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?

9
  • This is a great answer, encompassing all the knowledge I found a little bit scattered around. Just one question: where did you find/how did you choose 32;16~ and why is it not matching with the emacs binding below? I understood the origin of the numbers but I would like to ask you whether you can link to a mapping table somewhere for number1 and number2. Dec 27, 2014 at 11:16
  • @AndreaRichiardi I picked 32 because it's the ASCII/Unicode value for the space character and 16 because standard bindings use values up to 8 for Ctrl, Meta and Shift. The 33 below was a typo. You can pick anything as long as it doesn't conflict with other keys. Dec 27, 2014 at 15:17
  • I had to bind the sequence to a function so I momentarily solved with (global-set-key "\033[32;16~" 'set-rectangular-region-anchor). But to be complete, I tried your define-key above, but I couldn't then trigger (global-set-key (kbd "s-SPC") 'set-rectangular-region-anchor). It was just printing ;16~ in the buffer. It would have been nice to bind that sequence to (kbd "s-SPC") so that I have the key sequence bound and I can re-assign it to other commands. Dec 27, 2014 at 17:49
  • C-h l returns the following on pressing s-SPC now: ESC [ 3 2 ; 1 6 ~ but emacs still complains that M-[ 32 is undefined Dec 27, 2014 at 18:23
  • @AndreaRichiardi After fixing more typos in the xterm part (which you seem to have fixed since Emacs receives the intended character sequence), it works for me. Does it work if you use function-key-map instead of local-function-key-map? It may be a matter of when you set local-function-key-map, as it's terminal-dependent. Dec 27, 2014 at 19:18
1

If you're willing to install an Emacs package and configure your terminal, I wrote an Emacs package which can teach Emacs and terminals how to properly recognize all PC keyboard keys and modifier key combinations:

https://github.com/CyberShadow/term-keys

Encoding the Super modifier is not enabled by default, but the package is customizable and allows configuring which modifier key combinations are encoded. To enable encoding key combinations that include the Super modifier, set term-keys/want-key-p-func to a function which returns t when (elt mods 3) is non-nil (see the default implementation for an explanation).

1

I have the following in my init files:

(global-set-key [f7] nil)
(global-set-key [f8] nil)
(define-key function-key-map [(f7)] 'event-apply-super-modifier)
(define-key function-key-map [(f8)] 'event-apply-hyper-modifier)

This allows me to send super and hyper keys using the function keys.

0

Short answer: use Konsole.

Explanation:

  • Emacs' local-function-key-map has an entry mapping C-x @ s to event-apply-super-modifier.
  • Konsole maps Super (the Windows logo key) to the escape sequence ^X@s (which doesn't seem to be documented anywhere, I discovered it by chance). You can try this by opening Konsole and pressing e.g. Ctrl-v Win-o.
1
  • Thanks for the answer but changing the term is not the solution I am looking for. Jun 27, 2018 at 4:30
0

Inspired from @ubolonton's answer, I was able to bind Super and Hyper (and add a supplementary Meta key) in urxvt with those 4 lines of ~/.Xdefaults:

URxvt.keysym.XF86AudioPlay: string:\030@m
URxvt.keysym.Menu: string:\030@s
URxvt.keysym.Muhenkan: string:\030@h

and out of luck I dind't had to touch anything else to make it work.

Explanation:

On my keyboard (a TypeMatrix), under Shift_L, z, x, c I have: ctrl, XF86AudioPlay, Menu, Muhenkan (weird keyboard), so by binding the strings C-x @ m to XF86AudioPlay, C-x @ s to Menu and C-x @ h to Muhenkan, I have, from the left to the right: Ctrl, Meta, Super, Hyper.

(If you have the exact same keyboard as mine and want to try it out, you first need to set your keyboard to its 106-keys layout by pressing Fn-F3, else the 4th key is bound to Meta-tab.)

Your Answer

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

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