Changing your caps lock into Ctrl in X

Have you ever been irritated that there’s a big key between tab and shift that you never use? If not, sometime in the future your left little finger might start complaining about having to reach all the way down to the Ctrl key every time you’re moving around in an emacs buffer. This small tip will let you redefine your Caps Lock into a Ctrl key in X.

Of course there is a solution for this. The X window system wasn’t designed by a gang of morons so the key mapping system is quite flexible, though a little bit hard to understand. I won’t say I understand it :)

So, let’s say you wan’t to change your Caps Lock into a Ctrl key. The first thing to do is to glance through the manual page of xmodmap. When you’re done with that you’ll understand a little bit more of what’s happening. The next thing would be to execute the xev from an xterm or similar. Try putting the cursor in the window popping up, and press some keys up and down. A lot of information will pass by in the xterm you started the xev from. It will most probably look something like this:

KeyPress event, serial 21, synthetic NO, window 0x4800001,             
    root 0x36, subw 0x0, time 921569335, (87,93), root:(796,823),      
    state 0x0, keycode 66 (keysym 0xffe3, Control_L), same_screen YES, 
    XLookupString gives 0 characters:  ""                              
                                                                       
KeyRelease event, serial 21, synthetic NO, window 0x4800001,           
    root 0x36, subw 0x0, time 921569438, (87,93), root:(796,823),      
    state 0x4, keycode 66 (keysym 0xffe3, Control_L), same_screen YES, 
    XLookupString gives 0 characters:  ""                              

The interesting parts here is ‘keycode’ and perhaps what’s inside the parenthesis. Be sure to note the keycodes of the keys you’re interested in. 66 is the keycode of my Caps Lock, depending on you hardware yours might be something else (most probably it’ll be 66 on a PC). Now lets do some real action! You might want to start with executing xmodmap -pke and redirect the output to some file. That way you might be able to restore your keymap without restarting your X. After that, do this:

xmodmap -e 'keycode 66 = Control_L' 
xmodmap -e 'clear Lock'             
xmodmap -e 'add Control = Control_L'

And test if that did the trick. If you don’t get the desired effect, try some other combinations. You may want to put your Caps_Lock somewhere else, find a decent key and do this:

xmodmap -e 'keycode 117 = Caps_Lock'
xmodmap -e 'add Lock = Caps_Lock'

117 is one of the windowskeys on my Keyboard. Find a good key with xev as described before. I assume you don’t want to do those commands every time you restart your X. (Personally I don’t restart X more often than once a week or so). The solution to this is to put something like this:

keycode 66 = Control_L  
clear Lock              
add Control = Control_L 
keycode 117 = Caps_Lock 
add Lock = Caps_Lock    

(ie, the above commands without the xmodmap before each line). in a file called .Xmodmap or .xmodmap in your home dir. Then make sure the command xmodmap .Xmodmap is executed somewhere in your X startup files. If you have a good distribution that’s already automatically done. Debian for example will xmodmap a file called .Xmodmap. Check out the scripts in /etc/X11/ to see if it’s done in your dist, and what the filename should be.

Written on November 7, 2005