How to make Meta keys work

Last Updated: Wed 19 Jan, 2000
© Copyleft: Stephen Riehm 1991 - 2000

Warning: The information in this file is a result of my hard won experience. All I know with respect to terminal handling is what I have seen - and I haven't seen the back-end of all this. If you find any inacuracies in this file, please let me know!


Meta Keys - Preamble

These are some of the most useful, and at the same time, most construed and screwed up features of the modern keyboard since computers appeared on the earth. The problem is that everyone has a different view of these babies and what they are for, and no-one can agree (or even bothers to ask) on a suitable standard.

It all started with typewriters - remember them? You had a key for each letter of the alphabet. Typing the key on its own would produce the appropriate letter. Doing the same while holding down the shift key would produce the same letter, in upper case. Simple and effective. With the advent of computers, the keyboard took on a new role. Not only was it used for typing text, it was also used to control programs. Soon the control key was invented for controlling programs.Then there was the Alt key for alternate functions. At various points in history we then had any number of Super, Meta, Windows, Apple, Menu, Alt-gr and other keys. Each was designed to do something special, some came and went, others have stuck. (Like the Caps Lock key - who needs it? I personally hate it! Please, someone, invent a keyboard without a key that has a lock function!)

So today, the normal keyboard is left with control keys and alt keys. Neither are intended to be used for entering text, rather, they are supposed to help you control programs, however, every key press has to generate something which can be passed on to the program, and they all do. So far, so good.

So where's the problem?

Most computers today use ASCII or variations thereof. This means that pressing the letter a generates the ASCII code for the letter a, in this case, 0x61 in hexadecimal. Typing a shift-a generates the code for a capital a, which is 0x41 - or 0x61 - 0x20. control-a generates the ASCII code 1, or 0x61 - 0x60. What happens when you type alt-a? To answer this, you essentially have to ask the system you're using! Everyone does something different! HP for example will generate something like 0xD3 (211 if you're still counting with your fingers and thumbs), on windows you might get 0xE1, and on AIX you get a whole sequence of characters (a so-called escape-sequence). What a mess!

a Meta key is not an Alt key!

When the Meta key was invented, it was intended to let the user type more than just the 128 characters that make up the ASCII character set. Character sets such as latin1 which have characters for use in non-english languages, such as accents etc. generally use ASCII for the first 128 characters, i.e.: a common base, and then have 128 special characters on the end, giving you 256 characters in total (in theory at least). The Meta key simply adds 128 to the code of the character you are typing. Mathematically, it's very simple to work out which key you need to press to get a given result, if you know the code of the character in question. What you see on the screen is a different matter altogether (it depends completely on the character set you are using to start with!). However, this mathematical correlation is exactly what makes Meta keys so useful, in contrast to Alt keys which are nothing more than chaos.

So, why not simply use Meta keys all the time? You could still use the generated codes to drive menus, special functions etc, and you could also use them to re-create codes you've never typed before! Unfortunatly, the computer vendors of today, in their infinite wisdom, pre-configure their systems with Alt keys. Meta keys may or may not be supported, and are almost definitely not well documented. Such is life!

It get's stranger and stranger...

Back in the days when Richard Stallman was developing his emacs text editor, he also discovered and liked Meta keys. So much so that without Meta keys, it is virtually impossible to use emacs. Since there were computers around which didn't have Meta keys at all, he added the ability to type Meta characters by first typing an escape character, and then the character you want. To type a Meta-a, you would thus type a. You have generated two characters, but emacs understands them as a single sequence.

This isn't that bad actually, however, vi and vim use the escape key to change modes - so a tell's vi to change to command mode, and then append text after the cursor. But this is not so bad, vi doesn't make use of Meta keys.

Now there's a terminal program called rxvt which automatically converts meta keys, to escape sequences! It's really bizarre! When you type a Meta-a on a system which understands Meta keys, rxvt turns it into a before giving it to the programs at the other end. This throws a real spanner in the works, because you can map your keys to your hearts content, and rxvt will constantly convert them back to escape sequences. I haven't found a way around this yet, but until someone finds a solution, using the Meta-base macros in an rxvt window simply wont work - sorry.

What to do?

If you're using X11 on a unix system, there's a wonderful program which you can use to tell X11 that you would rather use Meta keys instead of Alt keys - the program is called: xmodmap. But first, let's check if you really need to use it.

Windows doesn't seem to make a distinction, and their Alt keys seem to work like Meta keys - lucky break.

If you're using other systems, you may not have Meta keys, but if you blindly use the codes that are generated, however, working out which key you pressed later will be a real challenge. If you comment the mappings you use, then you should be OK

How to check if I already have Meta- or Alt-keys (Unix)

You might like to try, cat with stdin (use ctrl-d to get back to the shell prompt):

    % cat -vte
    

then simply type a few characters with the Alt/Meta key pressed (as if it was a shift key), and then hit <Return> or <Enter>. If you see something like:

    ·’“$
    M-aM-bM-c$
    

you're in luck! - you can use the macros straight away!

If you see M-? style output, but the characters don't match up, i.e.: typing M-s gives you a M-b, then you should jump to the next section.

If you only see strange characters, you'll need to use od to see what's happening, like this (you'll also have to use ctrl-d to get out of od):

    % od -x
    

Again, type Meta-a Meta-b Meta-c and then hit <Return>, with a little luck you'll see something like this:

    0000000 e1e2 e30a 0000004
    

The output shows you the hexadecimal numbers associated with the keys you typed. The good thing is that you can easily check if a meta key is related to the same key without holding Meta. Here is the output of typing a <M-a> and then ctrl-d (the space is easy to see, it is the number 0x20)

    0000000 6120 e100 0000003
    

So here you can see that typing an a gives your program the character 0x61, typing a Meta-a gives 0xe1 which just happens to be 0x61 + 0x80!!

If you work your way through the alphabet, you should see a very simple pattern emerge, if not - you've got Alt keys.

If you still can't figure out which keycodes are being sent when you hold the Alt/Meta keys down, you might like to give xev a try, if it's on your system (not on a lot of commercial systems unfortunately). This program pops up a little window, simply move the mouse into the window and start typing keys, you'll be able to see exactly what X11 makes of those keys as you type them, but the output is pretty hard to read (there are 2 "events" for every key press, and 4 events for key presses involving shift, control or meta keys - nothing like being pedantic hey :-)

How to turn my Alt keys into Meta Keys?

If you're lucky:

The first thing to do is to find out the character code of the Alt keys. You can do this by typing the following command:

    % xmodmap -pk | grep -i alt
    

in my case, this gives me:

    keycode 68 = Alt_L
    keycode 70 = Alt_R
    

but your machine will very probably be different (at least the keycode number should be different)! All you need to do is replace the Alt_ bit with Meta_. X11 will change the bahaviour of the keys depending on this definition! Now you can create a file called $HOME/.xmodmaps, with something like this:

    ! Alt Keys are useless, meta keys are better
    keycode 68 = Meta_L
    keycode 70 = Meta_R
    clear mod1
    add mod1 = Meta_L Meta_R
    

OK, now for the big moment, type the following commands, and see what happens: (the lines beginning with a # can be ignored)

    # backup the original modmaps, just in case something goes
    # really awry
    xmodmap -pke > $HOME/.xmodmaps.original
    # load up the new xmodmaps
    xmodmap < $HOME/.xmodmaps
    # start a new xterm
    xterm &
    

and see if the tests above work. If they do, you can try the macros out.

Now all you need to do is make these changes permanent. You have to run the above command every time you log in, however, you can automate that by adding the commands to your .xsession or .xinit file - i.e.:

    xmodmap $HOME/.xmodmaps
    

(By the way, if you want to use the Alt key for something else, i.e.: a window manager key (for popping up menus, jumping between windows without using the mouse etc), you can - the window manager has no problems with it!)

If you're unlucky:

Some machines need a little more coaxing. If that's the case for you, you might like to try my little xmodtable perl script (perl 4.036 or greater!) which I've included for good measure. I wrote this years ago when trying to work out how best to map the keyboards on the old HP machines. It worked for me then, it seems to work now, but I haven't used it for a long time. Otherwise, you've essentially got to "hand map" all the meta keys, which should keep most people busy for at least an evening or two :-)

First, back up the existing mappings - just in case you want them back again in a hurry. (which you probably will if anything goes wrong - because you wont be able to type properly without them!)

    xmodmap -pke > $HOME/.xmodmaps.original
    

then run the script, like this:

    ./xmodtable | sed 's/Alt_/Meta_/' > $HOME/.xmodmaps
    

and then load up the new mappings:

    xmodmap $HOME/.xmodmaps
    

if you're still a little lucky, starting a new xterm now should result in a "working system" (i.e.: try the tests above again).

If not, I'm sorry, but I wasn't able to help this patient :-(

Good Luck!

Stephen Riehm