What is bash’s meta key?
I tried to use xmodmap to map META_L to the MENU key but it doesn’t seem to be accepted by bash as the meta key. So, I am wondering how these components (keyboard, X, xterm, bash) relate to each in regard the the Meta- and Super-Keys. Any explanation would be appreciated. Let me put this another way. The bash man page says, for example, that the function yank-nth-arg is bound to M-C-y and it works when I press Esc-Control-y . But I find this a bit cumbersome. So how can I make bash accept another key as Meta (like the Menu ) for all its default bindings?
Is your MENU key even noticed by X? What window manager are you using — some of them can help you redefining your keys.
3 Answers 3
The mapping from keyboard keys to modifiers like Meta and Control is handled by the X server (i.e. the low-level part of the GUI). This mapping can be manipulated through the old-style xmodmap command or the new-style XKB interface, or through a GUI configuration tools that uses one of these under the hood.
By default, on most setups, the Meta key is the key labeled Alt . This is because historically, many unix workstations had a key labeled Meta where PCs have a key labeled Alt . So if you have a binding for M-C-y , press Ctrl + Alt + Y .
To check what your current bindings are, start the xev program in a terminal. With the xev window focused, press keys; you’ll see a transcript of the generated events in the terminal.
The communication between terminal emulators (or terminal devices corresponding to a physical terminals) and applications uses characters. When you press A , the terminal receives the information “ A key, no modifier”, but what it sends to the application running in the terminal is the character a . When you press a function key like Up or F1 , there’s no corresponding character; the terminal sends a character sequence beginning with the escape character (byte 27, sometimes written \e or ^[ ). When a terminal emulator such as xterm received a key press event with the Meta modifier, it translates that key to an escape character followed by the key’s underlying function, e.g. \ea (escape, lowercase a) when you press Meta + A .
The key event is generated by the X server (as configured by xmodmap ), and is sent to your X application. Your window manager could intercept this before it is sent to xterm. XTerm, in turn, translates the event to some bytes and sends the bytes to the pseudo-tty allocated by your shell, bash.
Please note that not all events are translated directly to bytes by XTerm. For instance, the Ctrl and shift keys, by themselves, generate X keyboard events, but XTerm sends nothing to the shell (or other application) running within it. This also includes the Meta key by itself, but not the Menu key. However, the event generated by pressing Meta — e will make XTerm send either the byte 0xE5 or the two-byte sequence 0x1B 0x65, depending on the XTerm.VT100.metaSendsEscape resource setting.
Bash’s meta key originally was defined like this ( lib/readline/ChangeLog ):
Mon Jul 13 11:34:07 1992 Brian Fox (bfox@cubit) * readline.c: (rl_variable_bind) New variable "meta-flag" if "on" means force the use of the 8th bit as Meta bit. Internal variable is called meta_flag.
That happens to be the way it is interpreted with ncurses and xterm. A few terminals provided for making this feature optional (that aspect was not widespread. Most of the terminals you will encounter use hardcoded behavior (and not very interesting). The terminfo manual page documents these terminal capabilities:
has_meta_key km km Has a meta key (i.e., sets 8th-bit) meta_off rmm mo turn off meta mode meta_on smm mm turn on meta mode (8th-bit on)
If the terminal has a «meta key» which acts as a shift key, setting the 8th bit of any character transmitted, this fact can be indicated with km . Otherwise, software will assume that the 8th bit is parity and it will usually be cleared. If strings exist to turn this «meta mode» on and off, they can be given as smm and rmm .
A different feature, prefixing an escape character in response to the Alt key was incorporated into some terminal emulators. Bash (actually the readline library) documents that usage in its changelog from 2004:
lib/readline/callback.c - use _rl_dispatch_callback and a chain of _rl_keyseq_contexts to simulate the recursion used to decode multicharacter key sequences (even things like ESC- as meta-prefix
Meta is a special case of a modifier key. Like control and shift , you press it at the same time as another key and expect to see something different from pressing the key by itself. X provides for modifier keys by assigning a bit in the modifier value passed along in the X event for the key. Key presses can be multiple X events; X provides functions for combining these events while retaining the modifiers.
X also defines symbols for each of the keys that might appear on your keyboard. It provides for other values (such as Unicode) by special handling in the functions which combine events.
But «meta» is a special case.
X applications have no meta key, except by convention. X does not have a definition for the meta key, or the meta modifier. Conventionally, terminals look for the Alt -key and/or one of the modifiers known to xmodmap , e.g., mod2 . The later xkb feature complicates things (but provides no improvement relative to this discussion) by providing another layer of information to find the Alt key.
Convention of course can take you only so far, given that neither xmodmap nor xkb know anything in particular about meta. xterm, for instance, is configurable, and not all users want to configure meta in the same way. For instance, Alt may not be the intended meta-key, e.g., if it is used in the translation resource. Another key may be the meta key, but users (particularly those using escape sequences in bash) may want an escape character sent when they press Alt . But keep in mind that unless it is configured as a modifier none of that happens: xterm does not combine events by itself.
xterm has several resource settings (documented in the manual page):
- altIsNotMeta and altSendsEscape (added in 2007).
- eightBitInput since 2006 corresponds to the original sense of meta mode, and there is an escape sequence defined for this which provides the smm and rmm (set/remove meta-mode) terminal capabilities.
- eightBitInput was modified in 2003 to take UTF-8 into account by shifting the decoded Unicode values by 128 rather than the raw input bytes.
- metaSendsEscape dates from 1999
- eightBitInput is much older than metaSendsEscape . That has implemented a choice between meta mode (adding the eighth bit) or prefixing a key with escape since X11R4 (1989). But the feature was determined at startup: it was checked during initialization to determine whether the input was set to permit 8 bits or only 7. After that, it did not change.
Some people equate the two (8th-bit and escape-prefix), referring to the latter as meta mode. Depending on your point of view on the matter, the eightBitInput resource setting of xterm is part of the solution to getting a workable meta-key.
What are the Meta, Super, and Hyper keys?
In System → Preferences → Keyboard → Layout → Options → Alt/Win key behavior in older Ubuntu versions or in GNOME Tweaks → Keyboard & Mouse → Additional Layout Options → Alt and Win behavior for Ubuntu 17.10 and later, there is a menu similar to the one in the following screenshot: What do Meta, Super, Hyper mean?
1 Answer 1
Meta, Super and Hyper are modifier keys that modify the key’s function. They are specific to the Symbolics Space Cadet Keyboard used on Lisp machines back in the day. Their function is sometimes emulated using other keys.
- Meta The Meta key is not found on modern keyboards. Its use is sometimes emulated with AltGr (on some international layouts) or the right Alt key on the others. In addition:
- Sun keyboards have a meta key ( ◆ ) as well
- Emacs calls Esc the Meta key
- Super The Super key is equivalent to or the ⌘ (command) key. In Ubuntu, it’s just another name for .
- Hyper Hyper is the fourth (counting Ctrl ) and last modifier on the Space cadet keyboard. In Ubuntu, its function is undefined (I think), but it can be mapped, as in the screen shot above, to , should it be needed.
Sun’s keyboard had different modifiers.
Note that GNOME and X.org implement many modifiers. Meta, Super and Hyper are not Linux’s way of dealing with any modifier keys, but merely three of them specific to the Space Cadet keyboard. There is also Menu, Compose, AltGr, Greek, Front, X and others.
However, by convention the Windows logo key is called Super, and it is not implemented as yet another modifier by X.org.
Furthermore, the definition of modifier keys depends on:
- The keyboard itself
- The keyboard layout
- The driver
- The operating system’s implementation
It’s therefore almost impossible to generalise this information. I’ve tried to be somewhat specific to Gnome/X.org using a Windows-style keyboard with one of the more popular layouts. To test your keyboard, you can run xev — its terminal output will tell you what modifiers have been detected for your keyboard.
The Macintosh keyboard has a completely different set of modifiers
Some additional information:
- The Windows key is also sometimes called Meta. E.g. in KDE.
- The Alt key is sometimes called Option, as this is its equivalent on Macintosh keyboards
- The Compose key (not a modifier, but a dead key), also called Multi, is often mapped to the Menu key or the right Windows key.
- The AltGr key I’ve mentioned is also called a third-level modifier. Each key on the keyboard has, potentially, four characters on it. The first level is the lowercase, then it goes uppercase, Alt + something, and Alt + Shift + something. For example, the four glyphs on my A key are a, A, æ and Æ.
As they’ve been addressed in the comments:
- The Fn is not really comparable. It’s a hardware key that’s not directly interfaceable by the operating systems. Pressing Fn and another key causes an entirely different code to be sent to the operating system. Examples for this include XF86AudioMute, XF86Eject and so on. The Operating system can’t detect whether fn is pressed or not.
- The SysRq key is traditionally a label on the Print Screen key indicating its function if used with the modifier Alt . It is sometimes, especially on newer notebooks, mapped to Fn + Alt + Print . In Linux, it provides a few Magic shortcuts.