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.


Anonymous said...

Thanks so much!! Everywhere I read that remove lock would work but it didn't. So frustrating! Then I found your site with the extra keycode 66 = command! Yay!

JH said...

If I didnt use wx as my gui wrapper I would totally do that! I find that typing wx.FONTFAMILY_DEFAULT can get mighty tiring while holding down the shift key. Also, how else am I supposed to punctuate screaming everything I say at the top of my lungs?

Chris said...

This appears to be just what I was looking for. I have a eee pc 900 netbook and the caps lock key randomly gets activated with no way to disable it. Sometimes it'll be a little iffy and randomly activate/deactivate while typing which is especially annoying on linux, lol. This only happens when running X though, so I knew there had to be a solution. I've done what you recommended with xmodmap and so far, it seems to have worked. Thank you very much for posting this.