Miscellaneous ephemera…

Mapping CapsLock to Escape in Ubuntu

Feb 21st, 2010

The CapsLock key is one of the best examples of persistent usability failure;
it continues to appear on just about every English-language keyboard, despite
the fact that —aside from no-one ever uses it— it is an active hindrance to
both the average typist and the sedulous coder…

As a matter of course, I remove it in my Arch machines with an entry in my
.xinitrc, but in Ubuntu/Gnome, there is no
such file. Consequently, there is this equally effective way of dispatching the
CapsLock, or in my case, mapping it to something
actually useful, Esc.

First, create the mapkey file in your home directory:

xmodmap -pke > ~/.xmodmap.mapkey

Then open the file and edit it:

vim ~/.xmodmap.mapkey

Look for the CapsLock key, on my machine it is 66 (you can use
xev
to locate all of your key codes), and then wreak your revenge. Just deleting
everything after keycode 66 =

will render it useless, which may suit your style. I change the entry to make
it another Escape key:

keycode 66= Escape NoSymbol Escape NoSymbol Escape

Log out and when you log back in, you have a useful key.

Updated for 10.04

The above hack was for 9.10 and earlier versions of Ubuntu.
I have no idea why, but it (alone) doesn’t work in Lucid Lynx.
You need a couple of extra steps:

Open System > Preferences > Startup Applications and add
two new items:

Clear the CapsLock key with xmodmap -e "clear Lock

Map it to Escape by pointing to your xmodmap file

xmodmap ~/.xmodmap.mapkeys

Now, whenever you login the script(s) will run and your CapsLock will map
to something actually useful, like Escape.