previous contents up next

Unix for Advanced Users

16. X and Simple X Configuration

16.5. Useful X tips and tricks

16.5.1. How do I swap the CTRL and CAPS_LOCK keys?

A common complaint among users is that the left Ctrl (Control) and Caps Lock keys seem to be in the wrong places. Sun places the Ctrl key above the Shift key and the Shift Lock key below it and to the right. Almost all other vendors, including the many PC keyboard companies, place Caps Lock above Shift and Ctrl below. Which way is right usually depends on which type of keyboard you used first. In any event, people often want to switch the effects of the two keys.

The program to do this is xmodmap. It is included as part of the standard X distribution. Though swapping the Ctrl and Caps Lock keys is its most common use, xmodmap is actually a general-purpose keymap-editing tool. It can be used, for instance, to remap an entire keyboard from QWERTY to Dvorak style. It can even change the mappings of mouse buttons.

xmodmap works by reading a file of encoded operations, each of which makes or unmakes a binding between a key on the keyboard and the symbol it should generate. The name of the file is arbitrary. The program must be called once per X login, so users typically include a line like xmodmap my_style_file in their .xsession or .xinitrc.

The canonical .Xmodmap file for swapping Ctrl and Caps Lock reads as follows:

remove Lock = Caps_Lock
remove Control = Control_L
keysym Control_L = Caps_Lock
keysym Caps_Lock = Control_L
add Lock = Caps_Lock
add Control = Control_L
The only problem with this file is that it swaps the two keys unconditionally, so that it will fix a keyboard with the "wrong" key placement, but will also foul up a keyboard with the "right" placement. If you frequently move among machines with different keyboards, this behavior can be very annoying. The simplest solution is to set both keys to emit the Control symbol. Caps Lock is almost never useful, except to people in chat rooms with very bad manners. To achieve this effect, use the following as the contents of the xmodmap input file:
remove Lock = Caps_Lock
keysym Caps_Lock = Control_L
add Control = Control_L

previous contents up next