Fixing the right alt key for a Debian woody console


Without fail every time I install Linux (my preferred flavor being Debian), I run into the same pitfalls over and over. Every time I end up finding solutions for these...eventually, only to forget them again by the next time I install.

In order to break the cycle, I'm documenting some of my more common problems, along with their solutions here. The first problem I'm going to tackle involves the right alt key.

Early on in a Debian woody install, we select a qwerty map for our keyboard.

However, after our system has been set up, Emacs does not recognize the right alt key as being a meta key when we are using an extended keyboard (PC104+) and a plain old console (sitting at the actual machine, no ssh, no telnet, no X).

The Solution

The solution to this is to remap the the keyboard in the operating system. Looking online, there is plenty of documentation on how to fix this problem under X, but little mention of how to do so when just using a plain console.

In the end, the Linux keyboard and console HOWTO proved to be the most useful document for diagnosing the problem.

Debian has a host of keymap files available, categorized by architecture and layout type (qwerty, dvorak, etc) under the directory:

/usr/share/keymaps

We can select any of the configurations using debian config by running the following command as root:

# dpkg-reconfigure console-common

When doing the above, the changes persist across reboots.

Or we can do the same thing manually by running the following command as any user:

% loadkeys [keymap-file]

The above command affects all virtual terminals, but the changes will not persist across reboots.

The is all well and good, but unfortunately, none of the provided qwerty keymaps (including us, us-latin1, us-intl.iso01, and us-intl.iso15) fix the right-alt meta problem, while maintaining a correct US keymapping for the rest of the keys on the keyboard.

Ultimately, the solution is to create our own custom keymap with the left alt key specifically remapped to our liking.

Step 1 - Dump our current keymap

First, we need to dump our current keymap to a file in a temporary directory. We can accomplish this using the dumpkeys command, as shown below:

% mkdir temp
% cd temp
% dumpkeys > mykmap

Opening mykmap in our favorite text editor, we can see that it is just a plain text file with keycodes mapped to commands:

keycode  16 = q

(we can ignore the more complex looking lines)

What we want to do is find out what keycode the left-alt key is sending and map it to the same command that the right-alt key is sending. How can we find out what these keycodes are?

Step 2 - Capturing the alt keycodes

To find out what keycodes are being sent when we press keys on the keyboard, we can use the showkey command. showkey will capture the keycodes of any keys we press and output them to the console (showkey terminates 10s after the last keypress, restoring the keyboard back to its normal function).

% showkey

For the right alt key, showkey outputs the following:

keycode 100 press
keycode 100 release

For the left alt key, showkey outputs this:

keycode 56 press
keycode 56 release

Step 3 - Remapping right alt

Looking out our mykmap file again, we see the following:

Keycode 100 (right alt) is mapped to a command named AltGr, while keycode 56 (left alt) is mapped to a command named Alt. We want right alt to act identically to the way that left alt is currently acting, so we switch its command to Alt and save the file.

keycode 100 = Alt

Step 4 - Loading the new keymap

Time to test out our changes. We can load the keymap we just customized using the loadkeys command:

% loadkeys mykmap

Now when we launch into emacs, we should see that the right alt key is now behaving as a meta key, the way we like.

Step 5 - Making the keymap permanent

Even though we currently have our preferred keymap installed, our changes are not permanent. When we reboot our system, the console will again revert back to our old keymap.

On bootup Debian loads a keymap file from the following location:

/etc/console/boottime.kmap.gz

All we need to do is place our customized keymap at that same location and Debian will automatically load it at boot time. Given that the current keymap has been compressed using gzip, it seems prudent to follow this convention with our own as well.

First we gzip our keymap:

% gzip mykmap

This yields a file named mykmap.gz.

Now, as root, we backup our old boottime.kmap.gz file, in case we should ever need to revert to it for any reason, and replace it with a copy of our new keymap:

# mv /etc/console/boottime.kmap.gz \
>    /etc/console/boottime.old.kmap.gz
# cp mykmap.gz /etc/console/boottime.kmap.gz

(be careful that the new file is named exactly 'boottime.kmap.gz', otherwise you may experience console problems after your next boot)

If you like, reboot your machine to test things out. Our right alt key should now be permenantly remapped.