You can kill the background for speed, if you wish.[x]

Monday, December 19, 2011

Disabling the Caps Lock key in Linux

My personal laptop has had its Caps Lock key disabled for a while, because I find the Caps Lock key largely useless, and since I've been using Vim regularly at work, I've found it worse than useless - it's been counterproductive. Because when you're typing in the normal world and you accidentally have Caps Lock on, you realize that all your letters are coming out capitalized, and you backspace them and move on. But in Vim, if you accidentally bump the Caps Lock key, you're just trying to move around and all of a sudden your lines are disappearing, help starts popping up, it won't let you save or undo, and you start jumping all over the screen. Every time my screen stops doing what my fingers are telling it to do, it takes me a few (often destructive) seconds to figure out what the hell is going on, and it's usually the Caps Lock key.

So, to that end, I need to once again remap my Caps Lock key, but don't remember how to do it, and have to go back to my own more informative but longer blog post from a while back about remapping keys.  But I figured it'd be a lot easier in the future (and possibly helpful to others) to write a post dedicated to disabling the Caps Lock key, and since a quick Google turned up wrongdumb or temporary ways to do it, I will be doing myself, and perhaps you, a favor.  I also thought it'd be good to have a post without all the blathering, but you see how well that worked out.

So, how to disable your Caps Lock key in Linux, the Right WayTM:
  1. Open (or more likely create) an .xmodmap file in your home directory (vi ~/.xmodmap)
  2. Add the line remove lock = Caps_Lock to it
  3. Save and close
Changes here will take effect on an X restart - if you want to disable it right now, you can run xmodmap -e "remove lock = Caps_Lock" in a terminal, and it will take effect for your current session.  If this doesn't work, try also running xmodmap -e "keycode 66 =" to unmap the Caps Lock key completely - I had to do this on my work comptuer for some reason, despite all the "helpful" tutorials elsewhere on the internet.  If you also find this to be the case, add keycode 66 = to your ~/.xmodmap as well.  It's worth noting that the spaces around the equals sign are important in all of these commands.  If for some really strange reason you don't have xmodmap installed, you'll have to do that first, of course, but it should come with just about any distro.

If you want to, like I did in my original post, remap Caps Lock to a more useful key (say, Esc, vim users?), you can do so by adding the following lines instead. Something like this is actually what I have in place on my personal laptop, as it's remapped to mod5, which gives me an extra key to use for things like Compiz mappings.
remove lock = Caps_Lock
keycode 66 = {target keysym} {target keysym}
The first is what to replace Caps Lock, the second is an optional key for when you hit Shift+Caps Lock.  {target keysym} is a semi-friendly name like "Escape" or "k", and is obtained from pulling up a terminal and either running xmodmap -pke and scanning/grepping through the resulting list or, my favorite, running xev and mashing they keys you want to remap to find the "(keysym 0x##, [keysym name])" in the output.  A nifty way to do the latter is running xev | grep keysym - that will just spit out the needed line.  With xev, just close the white box that comes up to stop things.

If you do want to know about xmodmap and the wonders it can work in rearranging your keyboard to your satisfaction, or you want to move your Caps Lock key, by all means check out my original blog post, which goes into things in more depth.