January 27, 2014

How to Kill the Caps Lock on your Linux Desktop

caps lock key removed from a keyboard

Didn't there used to be a straightforward, GUI-powered way to disable the Caps Lock key on one's keyboard? I remember it as being one of the first modifications I did as a Linux newbie that actually altered the way a piece of equipment performed (boy, was I proud!). But some time around Ubuntu 11.04 (yes, during the Unity Revolution) that function seems to have vanished. I still use the machinery of Ubuntu, but with the less restrictive Xubuntu as a desktop.

I really liked being free of the intrusive Caps Lock, since I'm not a very good typist; I look down at the keys a lot, and if I've hit the lock by mistake it can be half a line or more before I notice. Anyway, with Disable suddenly unavailable I did some searching and found that the function of the Caps Lock key could be replaced by adding a script to xmodmap.

Now my Linux smarts had increased some by then, but they weren't yet up to the do-it-yourself script level, so I did some more searching to find a ready-made script. The first script I found was to change the function of the key to a Shift only. It was simple; in Leafpad (or Gedit, for that matter), make a little file:

!
! Swap Caps_Lock and Shift_L
!
remove Lock = Caps_Lock
keysym Caps_Lock = Shift_L

Save it as a hidden file in your /home folder and give it a name that defines it as a runnable script: it could be .capless, .nocaps, .freeatlast, even .name, as long as that . in front of it is present. Then, whenever you want to assert your independence from that old fAMILIAR ENDLESS LINE of capital letters, just run:

$ xmodmap /home/yourname/.capless

in a terminal or, for fellow Xubuntians, set it up once and for all in your Application Autostart section.

That was all I needed, at least for a while, but when I started using a new low-profile keyboard I occAsionally would notice strAnge results. The caps didn't stay locked, but the overlap of my finger on lock key and letter key becAme an intermittent problem. (Well, maybe not that bad.)

I figured one good script deserved another, so I began to look into just how that sort of keysym changing really works under the hood. I checked out the man page for xmodmap and found I could access the entire keysym list by entering:

$ xmodmap -pk

That lists the keysyms in hexadecimal; at 7 keysyms per keycode and 255 keycodes, be prepared to scroll if you're looking for something special. I lucked out: right up at the top is 0x0000, for No symbol – Caps Lock doesn't do anything. I realized I had found the way those old-time keyboard-altering GUIs worked! On the spot I did my new Caps Lock-disabling script:

!
! Set Caps_Lock to no symbol
!
remove Lock = Caps_Lock
keysym Caps_Lock = 0x0000

and saved it as .killer in the Application Autostart part of the Settings Manager.

On a different machine where I run Lubuntu, it isn't quite as easy to set it up for automatic startup. Once you have the script written and saved, open

$ /etc/xdg/lxsession/LXDE

and open the autostart text. At the bottom, add the line

@.killer enable

Log out, log in again, and you will be caps-free for all your subsequent logins.

But if you're lazy like me, you can just do the script and put in the Run Program line the full:

$ xmodmap /home/emery/.killer

After a while it gets used to seeing that, and autocompletes.