MetaKeyProblems

Intro

If your Emacs does not recognize your Alt key as the Meta key anymore, then you probably have some other key bound to Meta. In some distributions, this is the Windows key, or a Penguin key on some keyboards. On Macintoshes meta is often the Option key, which often is caught for other purposes.

The ESC key can be used as a prefix, and often works without initial hassle. Well, if running EmacsOnTerminal, better see below.

On some ports, Emacs finds an existing Meta key and no longer treats Alt as the Meta key. You have the following options, then:

X-window things

Using the XKB Extension

XFree86 option

If you’re using XFree86 4, you can get a similar effect by putting something like this in XF86Config-4:

    Section "InputDevice"
    ...
        # See /etc/X11/xkb/symbols/alt
        Option "XkbOptions" "alt:leftaltismeta"
    EndSection

Other keyboard type

Select a pc101 keyboard type instead of a pc104. X doesn’t even know about the Windows/Menu keys anymore, and Emacs works you would expect it to. Obviously this trick only works if you don’t use the Windows/Menu/Penguin/whatever keys.

Use Super for mod4

Put the following into your ~/.Xmodmap file (create if necessary).

 keycode 115 = Super_L
 keycode 116 = Super_R
 ! Super_L is already mod4 on my system, but Super_R is not
 add mod4 = Super_R

This will make your Windows/Penguin key the Super key. Thus, Emacs notices that no key will be Meta and accepts Alt instead of Meta.

This is because Meta and Alt are different and you can bind things to M-a and to A-a and they should not be confused. But in the case where there is no Meta binding, it’s preferable to treat Alt as Meta since Alt bindings are much more rare than Meta bindings in Emacs.

Just clear mod4

An alternative to adding the Super modifier key is to just clear mod4.

Check your xmodmap by running xmodmap or ‘xmodmap -pm’. Assume the following result:

 shift       Shift_L (0x32),  Shift_R (0x3e)
 lock        Caps_Lock (0x42)
 control     Control_L (0x25),  Control_R (0x6d)
 mod1        Alt_L (0x40)
 mod2        Num_Lock (0x4d)
 mod3        Mode_switch (0x71)
 mod4        Meta_L (0x73)
 mod5        Scroll_Lock (0x4e)

Meta_L is in mod4… that’s not good. Just clear it by adding ‘clear mod4’ to .Xmodmap or running the command ‘xmodmap -e “clear mod4”’. And now everything is fine.

xmodmap background

There are several terms and concepts which can be confusing.

 * A keycap is the label on a physical key.
 * A keycode is the numeric value produced by a physical key
   (these may not be the same on two different systems even
   if they use the same software and keyboard layout).
 * A keysym is the meaning assigned to the key.  It is the logical
   value sent to applications when you press that key, is determined
   by the keyboard mapping, and can be changed with xmodmap.
 * A modifier is special state produced by pressing a modifier key.
   Modifiers don't do anything unless another key is pressed.  An
   example is the shift modifier produced while you hold down a
   shift key.  Which keys produce which modifiers is controlled by
   the modifier mapping, and this can be modified with xmodmap.
   There are eight modifiers available, three of which have static
   meanings (shift, control, and lock), and the five others have
   generic names (mod1 through mod5).
 * A modifier key is a physical key which has its keycode listed
   in the modifier map.  It will produce one of the eight modifiers
   while pressed.

Note there are two types of mappings xmodmap can handle: keycode to keysym and keycode to modifier. The latter are somewhat unique to X, and are the most common source of confusion. In fact xmodmap makes it somewhat confusing because it’s configuration directives for modifiers are based on keysyms, which it internally resolves into keycodes.

Also note that modifiers like Alt, Meta, Super, Hyper, NumLock, and ScrollLock don’t have fixed modifier names – they are reported to the application as one of the five generic modifiers. It is up to the application to determine the meaning of the modifier.

The way applications are supposed to do this is to inspect the list of keysyms for the keycodes assigned to that modifier. For example upon receiving a key press event with mod1, if mod1’s keycode list includes a keycode with a mapping to Alt_L, this would mean mod1 is the Alt modifier. Some applications instead follow static mapping conventions. For example mod1 might be treated as Meta regardless of the assigned keysym. This used to be true of Emacs, so you will see lots of advice about putting Alt, Meta, or Super on specific modifiers. This shouldn’t matter to current versions of Emacs.

However, to complicate things further, Emacs does implement two special cases. First, if there is no modifier with a keycode with a mapping to Meta_L or Meta_R, Emacs will use the Alt modifier as the Meta modifier. And if Alt and Meta correspond to the same modifier, it will only use the Meta modifier.

Here’s the output of xmodmap:

 ~$ xmodmap
 xmodmap:  up to 2 keys per modifier, (keycodes in parentheses):
 shift       Shift_L (0x32),  Shift_R (0x3e)
 lock        Caps_Lock (0x42)
 control     Control_L (0x25),  Control_R (0x6d)
 mod1        Alt_L (0x40)
 mod2        Num_Lock (0x4d)
 mod3        Mode_switch (0x71)
 mod4        Super_L (0x73),  Super_R (0x74)
 mod5        Scroll_Lock (0x4e)

As you can see, the Shift keys act as a shift modifier, the Control keys act as a control modifier, and the left Alt key acts as mod1 modifier. And in addition to that, you can see that the “Use Super for mod4” solution has been used in this setup. There is no mention of Meta for any modifier, so Emacs will use the Alt modifier (mod1) as a Meta modifier.

On some keyboards there is a real Meta key. In that case and in some other cases you may want Meta to be distinct from Alt. In that case you would need to take one of the modifiers (for example mod4), and assign Meta_L and/or Meta_R and remove any other keycodes.

Emacs only changes

If this problem exists only on X Windows, I seem to have some luck with setting x-alt-keysym to meta.

    (setq x-alt-keysym 'meta)

Or, if you want to use the windows key (or penguin key) as meta, you can do it by setting x-super-keysym to meta.

    (setq x-super-keysym 'meta)

OS Specific things

MS Windows

On MS Windows you can use the Windows keys as Meta keys. This allows normal menu handling through the keyboard using the Alt key. However to do that fully you currently need a slightly patched version of CVS Emacs. You can get this is from EmacsW32 web site.

Via SSH - use PUTTY

If you are using windows and access a remote unix or GNU box using a term, one good term program, IMHO, is PuTTY. Very small and compact, yet does all the right things.. and customizable. Alt is automatically Meta. Page up/Down get passed right through to emacs, etc. The only problem I have had to date with putty is that the ‘pcprint’ hasn’t worked for me in putty (tried when using ssh).. anyone? – DeepakGoel

GNU/Linux

Use Alt for the Windows Manager

On some systems (eg. Debian) the Alt key generates “Alt” and the Windows key generates “Meta”. Instead of changing things around, adapt to it and use “Alt” key combinations for the window manager (eg. Sawfish). If you use a lot of hotkeys in your window manager, this is recommended.

MacOS

If you’re in a hurry, ESC maps to Meta, so you can type ESC x instead of M-x. This even works over SSH, on any keyboard layout.

Terminal

When running emacs from Terminal, you can set Terminal’s preferences/settings/keyboard and check "Use option as meta key", and the option/alt key should properly behave as the Meta key. You can also remap the Command key to be a control key under System Preferences/Keyboard/Keyboard/Modifier Keys to use emacs in Terminal. The downside to this approach is that it does not retain the Command key’s functionality for use in other Mac applications,

If you use Iterm2, you should edit Iterm2’s profile/Keys, and check Left Alt Key to Esc+.

X11

The Apple X11 server application has the meta at the Option button, which is caught by the application itself for various purposes, and never passes on to the X11 client. Disabling Enable key equivalents under X11 under the input preferences is one easy way to fix this, but this will effectively kill a few useful keybindings of the X11 servers too.

For some reason under leopard (Mac OS-X 10.0 and higher), internationalization may be turned on. This will result in insertion of a non-ASCII character in the native EmacsForMacOS when you use the Meta key. For example, you may see a “phi” when you type M-X, instead of the behavior you want (this is discussed in 26.13 Coding Systems for Terminal I/O in version 23). To disable this emacs-unfriendly behavior, enter the line

    (set-keyboard-coding-system nil)

in the initialization (~/.emacs) file. (Does this also fix the related problem in Aquamacs Emacs, discussed below?)

You can then run emacs from X11 windows (Applications/Utilities/X11.app) with the option/alt key as the meta key. Furthermore, you may use the xmodmap command to remap the command key to Control only under X11. This has the advantage of allowing the command key to behave expectedly with other Mac applications. xmodmap can also ensure that the Alt/Option key is set to be the Meta key in emacs. You can do this with the following ~/.xmodmap file (the lines starting with exclamation points are comments and can be omitted):

 ! undo any meta keys already set
 clear Mod1
 ! keycodes 63 and 71 are the left and right COMMAND buttons adjacent to the spacebar; set them to be control keys
 keycode 63=Control_L
 keycode 71=Control_R
 ! keycodes 66 and 69 are the option/alt keys; assign them as Meta keys
 keycode 66=Meta_L
 keycode 69=Meta_R
 ! now tell X11 that the Meta keys act as the Mod1 (meta) modier key; that is, when they are pressed with KEY, it is the same as hitting M-KEY
 add Mod1 = Meta_L Meta_R
 ! tell X11 that the Control keys are Control modifiers, so when pressed with u (for example) it is the same as hitting C-u
 add Control = Control_L Control_R

Use the above file with the command

    xmodmap ~/.xmodmap

before starting emacs (for instance, from /etc/bashrc if you use the login shell option to xterm, i.e. xterm -ls in X11/Application/Customize for terminal).

You can fix c-h so it brings up help (instead of acting as another delete key) by adding these lines to ~/.xmodmap

  ! Tell X11 that the delete key (above the spacebar) should send a C-? (control questionmark) instead of c-h.
   keycode 59=127

However, you must then also execute the command stty erase ^?.This tells the terminal session that C-? should be used as a backspace/erase key, instead of C-h. Note that “^?” is NOT the caret followed by a questionmark; it is C-?. Generate ^? by either shift-control-questionmark, or if keycode 59 is already set to 127, by hitting the delete key. In emacs, you may generate it by typing C-q prior to either of those sequences. The two steps of reassigning the delete key (keycode 59) and using stty to make it the backspace should be performed together, i.e. from the /etc/bashrc file. Performing these two steps “frees” C-h to be available for emacs to use as the help key. [NOTE: If you use both emacs and vim, this may break vim’s backspace! One “fix” is to put

 :imap ^? <left><del> 

into your .vimrc file.]

Native GUI

When using native EmacsForMacOS, the variables mac-option-modifier, mac-command-modifier and mac-command-key-is-meta are useful for changing the behavior of different modifier keys.

In AquamacsEmacs, Option (Alt) is mapped to Meta - however, because Option is used to input a lot of non-ASCII characters such as ü or £ on a variety of keyboards, the special character input methods take precedence by default. You can deselect “Option key produces only special characters” in the Options menu to use Option as Meta, or you set the variable mac-command-modifier to ‘meta - you will lose functionality, of course. If you use a non-English keybboard layout, the emulation modes might be just what you want. While they leave the Option key mapped to Meta, they will allow you to input many common characters ([,],{,},\, etc.) directly with the Option key, just as you would otherwise. You can find them in the Options → Option Key menu - or use, e.g., M-x emulate-mac-german-keyboard-mode.

In CarbonEmacsPackage, you can set the variable mac-option-modifier to ‘meta when your option key is used to input special characters. See also mac-control-modifier and mac-command-modifier if you want to map the meta-key to another modifier key (Ctrl, for example).

Make sure you aren’t mapping meta to send another modifier key. Under System Panel → Keyboard & Mouse → Keyboard → Modifier Keys - make sure Option Key is set to Option.

If you want to use your option key as meta in your terminal on the mac, under Terminal → Preferences → Settings → Keyboard check off the check box “Option as meta key”

Best option in Emacs 23 is to revert to Emacs 22 settings (alt is alt, cmd is meta) with this snippet:

    (setq mac-option-key-is-meta nil)
    (setq mac-command-key-is-meta t)
    (setq mac-command-modifier 'meta)
    (setq mac-option-modifier nil)

CocoaEmacs has a group of customizable options named Ns, for adjustment of GNUstep/Mac OS X specific features. Some variables in this group can be set to either assign behavior to modifier keys, or remove it to make them behave as usual in Mac OS X. The variables are named ns-{control,command,altenate,function}-modifier. (Each has an alias of the form mac-{control,…}-modifier.) To make a Option (Alt) work as described for AquamacsEmacs above, set ns-alternate-modifier to none (No modifier) in the customize buffer and save the changes.

Console and Terminal

FreeBSD console

Option 1 - Use the Esc key

That is, press then release Esc then hit x to get M-x.

Option 2 - Use the us.emacs keymap

There is a keymap for Emacs that others report works for them, but it didn’t work for me. To try it, you can run

    kbdcontrol -l /usr/share/syscons/keymaps/us.emacs.kbd

Put

    keymap="us.emacs"

in /etc/rc.conf to use it permanently.

Option 3 - Use a customized keymap

Ideally a keymap could send meta with the Alt key. With the us.iso.kdb keymap and newcons, the left Alt does this, but the right Alt key does not. With the us.iso.kdb keymap and syscons neither Alt key works for me.

With newcons I created a customized keymap based off of us.iso.kbd. I simply replaced all instances of ralt with lalt.

With syscons, I use this ugly workaround until I can find a better solution. Again, I created a customized keymap based off of us.iso.kbd. First I changed the line with lalt and replaced all instances of lalt with meta. I did the same for ralt. Next, I used the Windows key on my keyboard to send alt. To find the scancodes sent for a key, you can use the misc/kbdscan port. In my case the Windows key sends the scancode 105, so I added

     105   lalt   lalt   lalt   lalt   lalt   lalt   lalt   lalt    O

to my customized keymap.

Finally, I added

     keymap="us.customized.iso"

to /etc/rc.conf to permanently use the customized keymap.

NetBSD console

For whatever reason, the Alt key does nothing for me. In /etc/wscons.conf i add these lines near the bottom:

    encoding us.metaesc #this makes meta send esc

I guess this may be more correctly solved by changing the wscons terminal types to something better than vt100, but I’ve not been able to figure out the correct naming.

OpenBSD console

Add the following line to /etc/wsconsctl.conf:

    keyboard.encoding=us.metaesc

You can also set it interactively using the wsconsctl command (but the setting will be lost unless you add it to /etc/wsconsctl.conf):

    wsconsctl keyboard.encoding=us.metaesc

Linux console

If your Meta key works in the console, but Meta + Control + something doesn’t work, it may have to do with your .inputrc assuming the console sends 8-bit characters. Take a look at the dumpkeys man page and friends. You will find that for most Linux kernels, some combinations of keys and modifiers are defined, but not for all -- because the kernel will only take around 256 different events.

XTerm

Before trying this make sure that Meta in other terms like aterm is working. The XTerm converts per default Metacommands. To use the Meta-key in XTerm edit follwing:

    ~/.inputrc
    set meta-flag on
    set convert-meta off
    set output-meta on

    ~/.Xdefaults
    XTerm*metaSendsEscape: true
    XTerm*eightBitInput: false

The metaSendsEscape flag can also be tried or toggled by holding CTRL and pressing mouse button one and then selecting the ‘meta sends escape’ option in the resulting xterm menu.

terminals

It might not be obvious that when using EmacsOnTerminal, at least certain popular DEC terminals in the absense of an Escape-key send the escape prefix sequence on C-3 or C-[. Often the Emacs term/ libraries configure the f11 to be an ESC too.

source hacking for xemacs

Locate src/event-Xt.c, find the lines

  /* If there was no Meta key, then try using the Alt key instead.
     If there is both a Meta key and an Alt key, then the Alt key
     is not disturbed and remains an Alt key. */
  if (! meta_bit && alt_bit)
    meta_bit = alt_bit, alt_bit = 0;

.. and disable the conditional.


CategoryKeys MovingTheMetaKey