Maps Caps Lock key to Escape

One thing that bothered me about vim for some time was the delay in getting access to command mode. Reaching up and over to Escape just felt unnatural. So why not bind this action to a closer key?

A good candidate is the Caps Lock key. I almost never need to use the Caps Lock key in normal use. Who wants to write in all caps? However, this key is much closer to the home row relative to the Escape key. It turns out there isn’t a way to do this in vim. Key bindings bind a command to a key not normally associated with that command. Binding a key to another key, regardless of what command that key maps to, is outside of vim’s purview.

To solve this, we need to change the mapping of the keycodes at a lower level. The following script will allow you to toggle this mapping on and off. Once toggled, it lasts across restarts. So you only need to toggle it again if you need to use Caps Lock for its original purpose. Its use is explained in the comments:

#!/bin/bash
#
# Call this script with "off" to maps the Caps Lock
# key to Esc.
# Call it with "on" to map Caps Lock to Caps Lock.
#
case $1 in
        off)
                echo "Mapping Caps Lock to Esc..."
                xmodmap -e "clear lock"
                xmodmap -e "keycode 0x42 = Escape"
                echo "Done."
                ;;
        on)
                echo "Mapping Caps Lock to Caps Lock..."
                xmodmap -e "keycode 0x42 = Caps_Lock"
                xmodmap -e "add lock = Caps_Lock"
                echo "Done."
                ;;
        *)
                echo "Usage: toggle-caps-and-esc {on|off}, turn the caps lock key on or"
                echo "off.  When it is off, the key acts as an Escape key."
                ;;
esac

Now you can have access to vim’s command mode much more easily. Just in case you really need Caps Lock, I suggest you symlink the above script in your home folder, or make an alias to it. Then you can quickly toggle it on and off.

Post to Twitter Post to Delicious Post to Digg Post to Reddit

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in CLI and tagged , , , . Bookmark the permalink.

3 Responses to Maps Caps Lock key to Escape

  1. spearbearer says:

    Great! This is just what I was looking for!

  2. Jeff says:

    I have `jk` mapped to Escape and CAPS mapped to Ctrl

    `jk` -> `Esc` : lets me keep my hands on home key all the time
    `CAPS` -> `Ctrl` : My pinky thanks me every day since I use GNU screen a lot and Ctrl-A is meta key for GNU Screen.

  3. Thanks for another informative site. The place else may just I get that kind of information written in such an ideal manner? I have a challenge that I am simply now working on, and I’ve been on the glance out for such information.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>