Monday, March 3, 2008

Remap useless Caps Lock key in X

Caps Lock is in my opinion one of the most useless keys on a keyboard. Unless you own one of those keyboards specifically designed for Linux, your keyboard most likely has a Caps Lock key located in a prime real estate area: right above the Shift key.

How do we remap the Caps Lock key to something more useful, say the Esc key? Why the Esc key? See my earlier article.

First, specify the new key mapping in the file ~/.Xmodmap:

$ cat >> ~/.Xmodmap
remove Lock = Caps_Lock
keysym Caps_Lock = Escape


Note that the file ~/.Xmodmap may not pre-exist in your distro. The above command will create the file.

Next, execute those new mappings by:
$ xmodmap ~/.Xmodmap


Test it out by opening a command window in X. Type in some words on the command line. Press the new Esc key(formerly Caps Lock), and then the key b. This should move the cursor back a word.

The above will do the key remapping for your X-Window environment. The key mapping is not changed for your console windows. We will save that topic for another day.

7 comments:

Anonymous said...

You can also put 'Option "XkbOptions" "ctrl:swapcaps"' in your keyboard section of xorg.conf to switch ctrl and caps.

Findingmysky said...

I've been looking at the Internet to find how to do things like this, but there's still one thing missing for me, and I wonder if you can help. I do want to remap my caps lock and replace it with Ctrl+I, which would send the command to turn on an italic font in my word processor, IM, etc. A second press would similarly turn it off. I've done this in Windows with Autohotkey, but haven't found a way to do this in linux yet. So, can xmodmap be used to assign multiple keystrokes to caps lock. Also, would it be possible to assign Shift+caps lock to replace the original functionality of the caps lock key?

Peter Leung said...

FindmySky,

As far as I know, xmodmap does not let you map multiple keystrokes to a single key.

Some people recommend the use of KHotKey (for KDE users), but I personally have not tried this program or any program like it.

Peter

Anonymous said...

XBindKeys is, in IMO, the best hotkey manager for Linux.

Anonymous said...

So, FindmySky, in GNOME or any other X environment, you can use XBindKeys.

Brendan Milller said...

I'd like to provide an update. In recent versions of gnome it is now possible to remap the caps lock key through the preferences panel.

Go to
peferences->keyboard->layouts tab->other options

In the keyboard layout options menu that pops up there are a number of choices for remapping the caps lock key. In particular, if you select "ctrl key position" you will be given a "make capslock an additional ctrl key" option, which is useful for emacs users.

If you are a vim user then check under "capslock key behavior" for "swap ESC and Capslock".

karimofthecrop said...

+1 for catphive's suggestion.

In yet even more modern gnome:
- apt-get install gnome-tweak-tool
- super: search for tweaks
- in keyboard & mouse tab
- select "Additional Layout Options"
- expand "Caps Lock behavior"
- select "Make Caps Lock an additional WHATEVER YOU WANT"

celebrate.