Skip to content

Instantly share code, notes, and snippets.

@tanyuan
Last active October 3, 2024 21:35
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.

@yyancy
Copy link

yyancy commented Aug 19, 2022

Add another option for Linux user: https://github.com/rvaiya/keyd It's a key remapping daemon for Linux, and it's really neat!

amazing tool! I will get it a try. Thank you!

@gentleqq
Copy link

I don't understand how to activate this on mac os? In karabiner there is no < Post escape if caps is pressed alone, left_ctrl otherwise> as written in the third paragraph. In karabiner you have to click "Import more rules from the internet (Open a web browser)", right? Please give me a hint)

@mshiyaf
Copy link

mshiyaf commented Nov 10, 2022

For anyone trying this on Fedora,
I found the following solution the best.
http://nelsonware.com/blog/2019/04/30/how-to-map-caps-lock-to-escape-and-control-on-fedora-via-caps2esc.html

@CNote1
Copy link

CNote1 commented Jan 6, 2023

I'm using Linux method. It works fine but I don't have Caps Lock option anymore. Can I map Caps Lock to Esc key somehow?

@Andrei-Aksionov
Copy link

@gentleqq I also was confused.
So yeah, you need to open this link a browser, then find

Change caps_lock key (rev 5)

there are 5 rules that you can import at once

And then in the application in tab "Complex modifications" find

Change caps_lock to control if pressed with other keys, to escape if pressed alone.

@riverandroad
Copy link

riverandroad commented Nov 3, 2023

when i use any xcape-related command, i failed and got something like:

WARNING: No keycode found for keysym Escape (0xff1b) in mapping Control_L. Ignoring this mapping. Failed to parse_mapping.

how can i solve this ?

@idontlikecreatingaccounts
Copy link

idontlikecreatingaccounts commented Nov 6, 2023

Having done this, my CTRL works as ESC, and CAPS LOCK works as CTRL when held, but CAPS LOCK pressed alone only registers as ESC some of the time. On VIM it's less than half the time. In Firefox it seems to be about 4/5.
It should not be a problem with the physical keyboard. I tried a "key test" website and it logs the CAPS LOCK every time, but only sometimes an ESC with it. It's also a brand new Thinkpad.

I'm using Debian 12.2. Any tips?

@gregbowers
Copy link

Game-changing hack! Remapping Caps Lock to both Control and Escape is a productivity dream. Say goodbye to accidental ALL CAPS and hello to an enhanced keyboard experience. Loving the flexibility across Linux, Mac, cissp certification and Windows!

@topcowmoo
Copy link

Thank you!!. I ♥ this!

@mohamedaasik1
Copy link

Looking to Business bank account in dubai ? Our expert consultants offer top-notch services for business setup consultants in Dubai, licensing, visas and more.

@vexanshop23
Copy link

While perusing your content on this website, I found it to be highly informative! Please continue to share valuable insights.
Vexanshop

@mohamedaasik-ops
Copy link

Looking to best seo freelancer in dubai? Our expert consultants offer top-notch services for business setup consultants in Dubai, licensing, visas and more.

@mohamedaasik-ops
Copy link

Looking to chauffeur services in dubai? Our expert consultants offer top-notch services for business setup consultants in Dubai, licensing, visas and more.

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