Danny Guo | 郭亚东

Remap Caps Lock to Escape and Control

 ·  548 words  ·  ~3 minutes to read

The best tip I ever got for switching away from a default setting was to change my normally useless caps lock key to function as both escape and control. On every OS, there is a way to make caps lock work as escape when tapped and control when held down. It’s one of the first things I do when I get a new machine.

This hack is especially useful for text editing. When I first started using Vim, I searched for a better way to exit insert mode. I happily used the ctrl+[ trick for months, but learning about remapping caps lock made Vim far more enjoyable to use. It should be just as useful for preventing Emacs pinky, and on Linux and Windows, most shortcuts involve control.

I also frequently hit escape just to close things. Escape and control are usually in the corners of the keyboard, while caps lock occupies prime keyboard real estate that it doesn’t deserve. Set up this remap for the sake of ergonomics and speed. You can remap escape to caps lock as well if you want to retain a caps lock button.

Linux

I’ve used xcape (check out this Ask Ubuntu answer) on Ubuntu and caps2esc on Arch. I originally tried to use xcape on Arch as well, but I ran into the same problem as the caps2esc author where the remap would need to be re-executed every time I woke my laptop from sleep. caps2esc has worked flawlessly for me.

caps2esc

Install the program. For Arch users, there is an AUR package.

Add this job to /etc/udevmon.yaml:

- JOB: "intercept -g $DEVNODE | caps2esc | uinput -d $DEVNODE"
  DEVICE:
    EVENTS:
      EV_KEY: [KEY_CAPSLOCK, KEY_ESC]

Start the process with systemd:

sudo systemctl enable udevmon

xcape

Install the program. On recent versions of Ubuntu, you should be able to just run:

sudo apt update
sudo apt install xcape

Next you need to remap caps lock to control. Check out this EmacsWiki page for options. For Ubuntu/GNOME, I use GNOME Tweaks ($ sudo apt install gnome-tweak-tool), which has a setting for caps lock as control under the “Typing” tab.

Now run xcape:

xcape -e 'Control_L=Escape'

To keep it working through system restarts, you can add that line to your ~/.profile or use any method for running a script on startup.

Mac

I use Karabiner with a configuration taken from this GitHub issue comment. After installing Karabiner, open ~/.config/karabiner/karabiner.json, and modify the rules array.

{
    "profiles": [
        {
            "complex_modifications": {
                "rules": [
                    {
                        "manipulators": [
                            {
                                "description": "Change caps_lock to control when used as modifier, escape when used alone",
                                "from": {
                                    "key_code": "caps_lock",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "left_control"
                                    }
                                ],
                                "to_if_alone": [
                                    {
                                        "key_code": "escape",
                                        "modifiers": {
                                            "optional": [
                                                "any"
                                            ]
                                        }
                                    }
                                ],
                                "type": "basic"
                            }
                        ]
                    }
                ]
            }
        }
    ]
}

ControlEscape.spoon appears to be another option. It depends on Hammerspoon.

Windows

I use AutoHotkey with a script from this Super User answer. Save it as a text file with a .ahk extension. To run it, just double click the file after you install AutoHotkey. To automatically run it after restarts, add a shortcut to the script to your Startup folder. Reference the docs for more info.

*CapsLock::
    Send {Blind}{Ctrl Down}
    cDown := A_TickCount
Return

*CapsLock up::
    ; Modify the threshold time (in milliseconds) as necessary
    If ((A_TickCount-cDown) < 150)
        Send {Blind}{Ctrl Up}{Esc}
    Else
        Send {Blind}{Ctrl Up}
Return

← Migrating from Jekyll to Hugo MoMath Hackathon 2018: Vortex Pool →

Follow me on Twitter or Mastodon or subscribe to my newsletter or RSS feed for future posts.

Found an error or typo? Feel free to open a pull request on GitHub.