Ok, as I promised, here is how to map CapsLock to Escape in Xorg. This is especially useful for folks who use vim, as CapsLock in on the home row, and Escape is not. Actually escape is that far away that you have to have really long fingers in order to hit escape without moving your hand from its usual position.

Paths and line numbers are given for Ubuntu 7.04 Feisty Fawn, so if you’re on another distribution or another version of Ubuntu, your mileage may vary.

What we’ll do is add an XkbOption to the keyboard configuration files. At first, add the following at the end of /usr/share/X11/xkb/symbols/capslock:

partial hidden modifier_keys
xkb_symbols "escape" {
    key <CAPS> {        [       Escape  ]       };
    key <ESC>  {        [       None    ]       };
};

This defines an option that maps the CAPS and ESC key codes to the Escape and None symbols respectively. If you don’t want to disable the original Escape key, leave the corresponding line out. Now let’s give it an option name: insert the following line to /usr/share/X11/xkb/rules/base, somewhere around line 810, where the other capslock lines are:

caps:escape          =       +capslock(escape)

Now let’s add that option to the keyboard configuration in /etc/X11/xorg.conf: Find the InputDevice section for the keyboard and add the following line to the section:

        Option          "XkbOptions"    "caps:escape"

Now restart X by logging out and typing Ctrl-Alt-Backspace at the login prompt. That’s it. CapsLock is gone and on its place Escape stood in.

I wasn’t successful in convincing the XKeyboardConfig maintainer Sergej Udaltsov to accept a patch for this, but I attempted only once yet :)