tail -f findings.out

« Fixing the function keys on the Apple Keyboard in Ubuntu

An annoying gotcha when viewing page source in Firefox »

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.

Share and Enjoy:
  • email
  • LinkedIn
  • Slashdot
  • StumbleUpon
  • Technorati
  • Netvibes

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

Possibly Related (no promises):

  1. Get useful image information on the command line
  2. More efficient HTML editing in vim
  3. Useful Bash functions to determine OS and more
  4. Viewing all users on a Linux system

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

Tags: , , ,
January 31, 2009 - 8:15 PM
1 comment »
Leave a reply

Subscribe without commenting

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.