How do I make Caps Lock work like Esc on a Mac?
Caps Lock is a toggle key that is basically a translator. You can query it's state but I would not rely on it being like other keys, that could lead to trouble.
The only thing I know how to do is to map Caps Lock to Control, or Option, or Command. This can be done via the Keyboard & Mouse pane of System Preferences. Click on "Modifier Keys" on the bottom left and you'll be able to remap Caps Lock, Control, Option, and Command, to any of those.
@Craig: This suggests that Caps Lock can be used as a normal -- that is, non-toggle -- key. On my MacBook, since I have re-mapped Caps Lock to Control, the Caps Lock light never lights up. It simply acts like the Control key.
It's possible.
Solution 1, from http://trueaffection.net/post/48417577/map-caps-lock-to-esc'>http://trueaffection.net/post/48417577/map-caps-lock-to-esc
- Download PCKeyboardHack and install it.
- Go to PCKeyboardHack in System Preferences.
- Enable ‘Change Caps Lock’ and set the keycode to 53.
Solution 2 doesn't involve patching the keyboard driver, but gives you a Vim specific solution.
OS X supports mapping the Caps Lock key to a whole bunch of keys, but you have to do it 'by hand', editting .plist files. The process is described in http://www.macosxhints.com/article.php?story=20060825072451882'>this hint. As addendum to that hint I suggest you first set Caps-Lock to None in the System Preferences, then you only need to change one value in the .plist file. Also, you can of course use the Property List Editor instead of going through the XML conversion steps.
The trick is to map the Caps Lock key to the Help key (code 6), which isn't on most keyboards. But if it is, it will be treated as the insert key, which you probably don't use anyway, since you ask about remapping your Caps Lock to prevent stretching your hands ;)
You can then map the Help and the Insert key to Esc in vim.
map <Help> <Esc>
map! <Help> <Esc>
map <Insert> <Esc>
map! <Insert> <Esc>
This will work for gvim (Vim.app). I didn't get it to work with vim in the Terminal and I haven't tested it with MacVim.
So, it's rather a complicated, half-baked solution or installing a third-party piece of hackery. Your pick ;)
Edit: Just noticed solution 3, if you're using MacVim you can use Ctrl, Option and Command as Esc. With the System Preferences it's trivial to map Caps Lock to one of those keys.
You can also use DoubleCommand to remap this, and other keys.
IIRC, it will map Caps Lock to Esc.
I came across this old thread in a search for the same functionality. So far, this utility is the one I have found that seems to work well (on 10.5.6), without much effort:
http://www.pqrs.org/tekezo/macosx/keyremap4macbook/extra.html
Edit: Just noticed solution 3, if you're using MacVim you can use Ctrl, Option and Command > as Esc. With the System Preferences it's trivial to map Caps Lock to one of those keys.
How exactly can you use Ctrl, Option and Command as Esc in Macvim?
Cheers, Dan
Having tried several of these solutions, I have some notes:
DoubleCommand will not allow you to swap esc and caps-lock.
PCKeyboardHack will allow you to map capslock to escape, but does not have the capability to map escape to capslock. This may or may not be sufficient for your needs (I know it is for mine).