Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
Is your MENU key even noticed by X? What window manager are you using - some of them can help you redefining your keys. – choroba Jan 13 '12 at 8:24

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.

What is the exact difference between a 'terminal', a 'shell', a 'tty' and a 'console'? may be useful background.

share|improve this answer

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.

share|improve this answer

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)

and explains the feature:

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.

Further reading:

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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