Skip to content

Instantly share code, notes, and snippets.

@dontdieych
Forked from ivyxjc/smart-caps-lock.md
Last active April 30, 2024 05:58
Show Gist options
  • Save dontdieych/e1c1a2a0283cd9b6d6d1ce09ca33b15d to your computer and use it in GitHub Desktop.
Save dontdieych/e1c1a2a0283cd9b6d6d1ce09ca33b15d 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

Option 1

xcape only working in X11 level. Don't recommend.

Option 2

https://github.com/rvaiya/keyd

Quick start

  1. Install and start keyd (e.g sudo systemctl enable keyd)

  2. Put the following in /etc/keyd/default.conf:

[ids]

*

[main]

# Maps capslock to escape when pressed and control when held.
capslock = overload(control, esc)

# Remaps the escape key to capslock
esc = capslock

Key names can be obtained by using the keyd monitor command. Note that while keyd is running, the output of this command will correspond to keyd's output. The original input events can be seen by first stopping keyd and then running the command. See the man page for more details.

  1. Run sudo keyd reload to reload the config set.

Recommended config

Many users will probably not be interested in taking full advantage of keyd. For those who seek simple quality of life improvements I can recommend the following config:

[ids]

*

[main]

shift = oneshot(shift)
meta = oneshot(meta)
control = oneshot(control)

leftalt = oneshot(alt)
rightalt = oneshot(altgr)

capslock = overload(control, esc)
insert = S-insert

This overloads the capslock key to function as both escape (when tapped) and control (when held) and remaps all modifiers to 'oneshot' keys. Thus to produce the letter A you can now simply tap shift and then a instead of having to hold it. Finally it remaps insert to S-insert (paste on X11).

Option 3

对于Arch及其衍生发行版,可以安装interception-toolsinterception-caps2esc。然后将下面的内容复制到/etc/interception/udevmon.yaml,然后执行sudo systemctl restart udevmon

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

caps2esc mode

  • 0:default
    • caps as esc/ctrl
    • esc as caps
  • 1:minimal
    • caps as esc/ctrl
  • 2:useful on 60%% layouts
    • caps as esc/ctrl
    • esc as grave accent
    • grave accent as caps

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. Find Post escape if caps is pressed alone, left_ctrl otherwise and click on enable.

The effect should apply immediately.

{
    "description": "left-option to left-control",
    "manipulators": [
        {
            "from": {
                "key_code": "left_option"
            },
            "to": [
                {
                    "key_code": "left_control",
                    "repeat": true
                }
            ],
            "type": "basic"
        }
    ]
}
{
    "description": "left-command to left-option",
    "manipulators": [
        {
            "from": {
                "key_code": "left_gui"
            },
            "to": [
                {
                    "key_code": "left_option",
                    "repeat": true
                }
            ],
            "type": "basic"
        }
    ]
}
{
    "description": "left-control to left-command",
    "manipulators": [
        {
            "from": {
                "key_code": "left_control"
            },
            "to": [
                {
                    "key_code": "left_gui",
                    "repeat": true
                }
            ],
            "type": "basic"
        }
    ]
}
{
    "description": "left-command to left-control",
    "manipulators": [
        {
            "from": {
                "key_code": "left_gui"
            },
            "to": [
                {
                    "key_code": "left_control",
                    "repeat": true
                }
            ],
            "type": "basic"
        }
    ]
}
{
    "description": "Post escape if caps is pressed alone, left_ctrl otherwise",
    "manipulators": [
        {
            "from": {
                "key_code": "caps_lock",
                "modifiers": {
                    "optional": [
                        "any"
                    ]
                }
            },
            "to": [
                {
                    "key_code": "left_command"
                }
            ],
            "to_if_alone": [
                {
                    "key_code": "escape"
                }
            ],
            "type": "basic"
        }
    ]
}
{
    "description": "Change Command+Tab to Control+Tab",
    "manipulators": [
        {
            "from": {
                "key_code": "tab",
                "modifiers": {
                    "mandatory": [
                        "command"
                    ],
                    "optional": [
                        "any"
                    ]
                }
            },
            "to": [
                {
                    "key_code": "tab",
                    "modifiers": [
                        "control"
                    ]
                }
            ],
            "type": "basic"
        }
    ]
}
{
    "description": "Change Control+Tab to Command+Tab",
    "manipulators": [
        {
            "from": {
                "key_code": "tab",
                "modifiers": {
                    "mandatory": [
                        "control"
                    ],
                    "optional": [
                        "any"
                    ]
                }
            },
            "to": [
                {
                    "key_code": "tab",
                    "modifiers": [
                        "command"
                    ]
                }
            ],
            "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.

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