How to map CapsLock to Control and Escape on Mac OS X

Make your use of Vim more efficient

Max Pechyonkin
5 min readDec 12, 2018

Background: Why You Should Give Vim a Try

I recently started using Vim, and I don’t regret it. I quickly fell in love with its efficiency and power. One of the goals of using Vim is to be able to do very complex and powerful editing solely on the keyboard, without a mouse, and also without moving your fingers from the home row of the keyboard.

Additionally, if you want to try Vim yourself, you don’t need to switch away from your current editor, you can simply install a Vim emulator and give it a try. Some examples of plugins include PyCharm (which I use), Visual Code Studio and Sublime Text. You can even get Vim-inspired keyboard-only navigation for a browser.

Typing in Vim without moving hands around works, with few exceptions: Vim heavily uses Escape and Control keys.

These keys are located quite far from the home row and typing Escape or any combination involving Control seemed very inefficient to me. I decided to figure out why and also try to solve this inefficiency.

A Little Bit of History of Computing

It turns out that back in the day, Escape and Control keys were positioned differently from today’s keyboards. Bill Joy, who created vi (predecessor of Vim), used a keyboard that had Escape key where modern keyboards have Tab key; Control key used to be positioned where CapsLock is today.

ADM-3A computer terminal. Bill Joy used one of these when he created vi. Note the positions of Escape and Control keys. Source.

After learning this, it all suddenly made sense to me. I had to put Escape and Control closer to where they used to be. One solution was remapping Tab to Escape and CapsLock to Control, just like it used to be back in the day. But Tab is used heavily these days, so I decided to not change it.

Another solution that I saw was to overload the CapsLock key, making it act as Escape when quickly pressed and as Control when held longer or pressed in combination with any other key. I decided to go this route. This post explains how to do it on a Mac. We will use Karabiner to map CapsLock to Escape and Control. Below is a step-by-step guide.

1. Install Karabiner

Go to the official website and install Karabiner.

Karabiner is software that allows to remap keys on your keyboard. It is very flexible, you can even set different keys to act differently depending on the keyboard you use, set different profiles for various uses cases and more.

2. Create a Complex Modification

Create a new file called custom-capslock.json and paste the following text into it:

{
"title": "Change caps_lock to Esc and Control",
"rules": [
{
"description": "Post Esc if Caps is tapped, Control if held.",
"manipulators": [
{
"type": "basic",
"from": {
"key_code": "left_control",
"modifiers": {
"optional": [
"any"
]
}
},
"to": [
{
"key_code": "left_control",
"lazy": true
}
],
"to_if_alone": [
{
"key_code": "escape"
}
]
}
]
}
]
}

Save the file and put it in the ~/.config/karabiner/assets/complex_modifications/ folder.

3. Apply the Complex Modification

Find the Karabiner icon in your menu bar and go to Preferences.

Go to Complex Modifications tab and click the Add rule button.

At this point, you should be able to see a custom rule called “Post Esc if Caps is tapped, Control if held.” with an Enable button to the right. Click it.

It should show now that the rule is enabled. Now, go to parameters tab and change the value of to_if_alone_timeout_milliseconds from 1000 to 500 milliseconds.

This parameter is the cutoff for distinguishing between Escape and Control in our case. If you press CapsLock alone for less than 500 milliseconds, it will be considered an Escape, and if you press it for longer, it will be registered as Control. However, if you press CapsLock in combination with any other key, it will be registered as Control regardless of the duration of the press.

4. Remap the Keys

At this point, we only changed the behavior of the Control key itself. Now, we need to remap physical CapsLock key to act as Control and physical Escape key to act as CapsLock (in case you really need CapsLock functionality).

Go to Simple Modifications tab and create 2 new simple modifications.

This is it! Now, you can be more efficient with your touch typing and keep your fingers on the home row, minimizing unnecessary movement.

Thanks for reading! If you enjoyed it, hit that clap button below and subscribe to updates on my website! It would mean a lot to me and encourage me to write more stories like this.

You can follow me on Twitter. Let’s also connect on LinkedIn.

--

--