Posts // Remap Caps Lock to Ctrl with xmodmap

Use the xorg tool xmodmap to remap the Caps Lock key to Ctrl on X Windows and run it automatically on startup

Note: Update November 2020

After an Arch update, xmodmap mysteriously stopped working so I replaced my Capslock remapping configuration with setxkbmap. If xmodmap still works on your distro, the following instructions should still work, but otherwise you might check out the setxkbmap instructions.

Archaic Keys

There are many underused keys on a standard PC keyboard. I have never used the Scroll Lock or Pause keys. These are archaic remnants of a time when basic tasks were so cumbersome that a key was dedicated to it. I breathlessly pressed the Pause key while writing this, half expecting it to freeze my computer, but alas, nothing seemed to happen. There is no official standard for the required keys on a keyboard. Apple has streamlined their keyboards and removed many of these artifacts. As an English speaking PC nerd I prefer to use a "standard" keyboard with all the semi-useless bells and whistles that have never chimed nor whistled.

The CapsLock key is one relic whose usage has been shunned by the consensus of electronic etiquette. The feeling that one is being yelled at when receiving an uppercase message has relegated it to fringe trolling and the occasional piece of legalese. The only socially acceptable form of use is when an elderly person who has NO IDEA THAT THEY HAVE PRESSED THE CAPSLOCK KEY NOR DO THEY CARE BECAUSE THEY HAVE JUST FIGURED OUT HOW TO USE THIS GODDAMN MACHINE AND THEY JUST WANT TO SEND AN ELECTRONIC MESSAGE.

Make Caps Lock Great Again

As an aging programmer with a growing list of pains and gripes I have come to appreciate the wisdom of keeping my hands securely centered on the keyboard, with my left index finger firmly planted on F and my right index finger on the J key. I find it awkward to stretch my pinkies from this position. This became a problem when I fell in love with Emacs a decade ago. Nearly every command was prefixed with some form of the Ctrl key. I started reading some forums and many users recommended remapping CapsLock to Ctrl. After a decade, I still marvel at the brilliance of this piece of advice.

Xmodmap and X Windows

I spend almost all of my time using applications in the i3 window manager running on an X Windows server. The xmodmap utility allows one to easily remap keys in X Windows.

xmodmap is sometimes included by default with an Xorg installation. Arch linux has a package called xorg-xmodmap that contains the xmodmap utility.

Xmodmap Configuration File

Create a file in your home directory named .Xmodmap.

Here is the contents of the configuration file:

! swap Caps_Lock and Control_R
remove Lock = Caps_Lock
remove Control = Control_R
keysym Control_R = Caps_Lock
keysym Caps_Lock = Control_R
add Lock = Caps_Lock
add Control = Control_R

This configuration maps CapsLock to the Right Ctrl key just in case you ever find yourself awake at 4 am defending the merits of "I Can Has Cheezburger?" against the idiocy that is "nyan cat". Press Right Ctrl to regain your super powers.

Running Xmodmap

You can directly use xmodmap from a terminal:

$ xmodmap ~/.Xmodmap

Automatically Run xmodmap

Some linux distros will automatically run your xmodmap configuration if you put it in your home directory and name it .Xmodmap. If you run Gnome or KDE you might be in luck. Other window managers will likely require adding the xmodmap command to a startup script.

It is important that you call the full path /usr/bin/xmodmap as your PATH variable may not be fully configured.

Automatically Run xmodmap in i3

I found that the easiest way to automatically run xmodmap in i3 was to add an exec command to the end of my i3 config.

I added the following to .config/i3/config:

exec --no-startup-id /usr/bin/xmodmap /home/<username>/.Xmodmap

Automatically Run xmodmap using .xinitrc

If you start your window manager using .xinitrc, you can put the xmodmap command in there as well.

/usr/bin/xmodmap /home/<username>/.Xmodmap
exec <your-wm-exec-command>

Note that the command is placed before the exec command. If you put the xmodmap command after exec it won't run.

Related Posts