Skip to content

Instantly share code, notes, and snippets.

@tanyuan
Last active March 26, 2025 00:15
Show Gist options
  • Save tanyuan/55bca522bf50363ae4573d4bdcf06e2e to your computer and use it in GitHub Desktop.
Save tanyuan/55bca522bf50363ae4573d4bdcf06e2e to your computer and use it in GitHub Desktop.
Smart Caps Lock: Remap Caps Lock to Control AND Escape

Smart Caps Lock: Remap to Control AND Escape (Linux, Mac, Windows)

Caps Lock 變成智慧的 Control 以及 Escape

  • 單獨輕按一下就是 Escape
  • 若按下時同時按著其他鍵,就會是 Control

這應該是 Vim 和 Emacs 的最佳解了!(Emacs? Bash 的快捷鍵就是 Emacs 系列的)

  • Send Escape if you tap Caps Lock alone.
  • Send Control if you press Caps Lock with another key.

For both Vim and Emacs world.

GNU/Linux

Step 1. Install XCAPE

Install XCAPE (xcape). (Linux utility to configure modifier keys to act as other keys when pressed and released on their own.)

Ubuntu:

sudo apt-get install xcape

Step 2. Run the command

# make CapsLock behave like Ctrl:
setxkbmap -option ctrl:nocaps

# make short-pressed Ctrl behave like Escape:
xcape -e 'Control_L=Escape'

The effect should apply immediately.

Step 3. Autostart

Append code from Step 2 to file ~/.xprofile to run the command when X starts.

macOS

  1. Download and install Karabiner-Elements, a powerful and stable keyboard customizer.
  2. Open Karabiner-Elements, select Complex Modifications tab, and click Add rule at the bottom.
  3. Click Import more rules from the Internet (open a web browser)
  4. Find Change caps_lock to dual keys, escape and left_control and click on enable.

The effect should apply immediately.

If you cannot find the rule, you can manually edit the config files at ~/.config/karabiner/karabiner.json, under profiles:

"rules": [
                    {
                        "description": "Post escape if caps is pressed alone, left_ctrl otherwise",
                        "manipulators": [
                            {
                                "from": {
                                    "key_code": "caps_lock",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "left_control"
                                    }
                                ],
                                "to_if_alone": [
                                    {
                                        "key_code": "escape"
                                    }
                                ],
                                "type": "basic"
                            }
                        ]
                    },
]

Windows

Option 1:

  1. Download and install AutoHotKey.
  2. Use the script CapsLockCtrlEscape.ahk.

Option 2:

Download and run dual-key-remap. (Note that in config.txt, CONTROL might needed to changed to LEFT_CTRL for some computers)

For both options, the effect should apply immediately.

@topcowmoo
Copy link

Thank you!!. I ♥ this!

@jtchen2k
Copy link

Thank you for sharing this, it's really helpful.

@yuchin1189
Copy link

For wayland users, check out keyd instead.

由於上述的 XCAPE 方法只支援 X11 的關係,如果你使用的 GNU/Linux 發行版搭配較新的 Wayland,可以用 keyd 達到相同的效果。

@rqdaA
Copy link

rqdaA commented Feb 22, 2025

This method only worked with US keyboard layout on Windows. With other keyboard layouts (in my case, Japanese), AHK did not work.
Please refer to this gist if you're using a Japanese keyboard layout (this may also work for other CJK layouts).

@alcoheca
Copy link

On OSX a simpler approach is https://hyperkey.app/ 2.5MB vs 30MB installer.

You don't actually use it's Hyper key functionality, just the ability to remap Caps Lock to Control and Escape on a quick press

Screenshot 2025-03-20 at 13 19 26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment