I’m Paul Herron, a full-stack developer and technical manager.
I focus mainly on back-end development with tools like Symfony
and devops with tools like Docker

Making a Vim 'save' key by remapping caps lock

by Paul Herron on 28 August 2013

Most Vim users, if they’re going to remap their caps lock key to do something more useful, seem to map it to Esc or to <Ctrl>. Either option is handy because those keys are so commonly used in Vim, so it makes sense to assign one of them to a big key that’s on the home row. It makes even more sense when you look back at the old terminal keyboard that inspired the Vi shortcuts, and see that’s where these keys started out.

All fine, but I’d argue the benefits from a setup like that are pretty marginal. I’m happy to keep reaching for those keys if I could just map caps lock to something a little more specialsed - namely to run the :w command.

As a web developer I need to hit save many times a day, and it always seems odd that Vim — such a champion of reduced keystrokes — makes me do this merry dance of <Esc><Shift>;w<CR> before I can view my changes in a Web browser. Certainly there are other options — I could rig something up to make Vim save the file when losing focus, for example — but that’s always felt a little clumsy to me. Maybe I’m editing a huge file which I definitely don’t want to save each time I lose focus, or maybe I want to keep focus solely on one file but save regularly to avoid losing progress.

So, I’m happy to keep saving manually; I just want to do it in fewer keystrokes. Here’s how to set that up very easily…

Remapping caps lock

OS X lets you remap caps lock, but sadly only to a handful of other modifier keys like <Ctrl> and <Shift>. That’s no good to us here, because Vim can’t bind a command to a modifier key alone. We need to map caps lock to a regular key instead, so we can attach a Vim command to it.

That’s easily done on a Mac with PCKeyboardHack. I used it to make caps lock act as the <F2> key on the (possibly naive) assumption that <F2> doesn’t do anything special in Vim, so repurposing it in this way won’t break anything.

Set caps lock to fire keycode 120 in PCKeyboardHack

Creating the mapping in .vimrc

Hitting caps lock should now be registering as a press of <F2>. That’s easily verified in Vim by pressing : to enter the command line, then pressing caps lock. You should see something like:


If that’s worked, it should be a case of entering a couple of mappings for <F2> in ~/.vimrc:

nmap <F2> :w<CR>" Bind caps lock to save a file (assuming PCKeyboardHack or similar was used to map caps lock to F2)
imap <F2> <Esc>:w<CR>" As above, but from insert mode

Finally, source that file to make these mappings take effect:

:so ~/.vimrc

It’s nice being able to just whack a big button to save your changes. It feels natural - so much so that I find myself doing that in lots of other apps too, even in places like Google Docs. I’m pretty sure I could declare some useful mappings in these other apps too, although I haven’t looked into this yet.

Back to homepage Back to blog listing