Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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.


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.)


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.


  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.


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.

Copy link

gustavetd commented Apr 10, 2018


Copy link

jtaxen commented Aug 31, 2018

This was very handy!

Copy link

sandangel commented Sep 20, 2018

could you please help with config Control_L + hjkl to arrow key on both linux and macOS?

Copy link

Abraxas-Knister commented Jun 23, 2019

I did this, I got "WARNING: No keycode found for keysym Escape (0xff1b) in mapping Control_L. Ignoring this mapping."

Copy link

Abraxas-Knister commented Jun 23, 2019

Surrendered, turns out I should have run an "setxkbmap -option" (notice empty argument) first.

Copy link

likern commented Jul 6, 2019

Does anyone know how to do that purely with XKB?

Copy link

yahkun commented Jul 24, 2019

Thanks, this is very helpful~

Copy link

bxiehq commented Jan 29, 2020

On Linux, I have encountered a situation where for some unknown reason the Caps Lock is turned on. You can toggle it by xdotool:

xdotool key Caps_Lock

Copy link

minhduc0711 commented Aug 11, 2020

What should I do if I want to use Escape as Caps Lock (on Linux)?

Copy link

shiorid commented Oct 13, 2020

What should I do if I want to use Escape as Caps Lock (on Linux)?

"ctrl:swapescape" xkb-option should work

Copy link

jackylx2008 commented Feb 3, 2021

very useful hints

Copy link

zerorooot commented Mar 19, 2021


Copy link

elsayedg commented Jul 9, 2021

Thank you

Copy link

zestime commented Jul 17, 2021


Copy link

acccounttest commented Jul 22, 2021

I did a similar approach to replace the key tab by caps lock without breaking any combination, adding double tab trigger to switch windows in metro context under Windows 8 but it's mandatory, can be commented out.

Copy link

raiguard commented Sep 16, 2021

This is amazing, thank you! It works perfectly!

Copy link

zstreet87 commented Sep 24, 2021

Surrendered, turns out I should have run an "setxkbmap -option" (notice empty argument) first.

I am having the same warning. How did you solve this? Thanks!

Copy link

zhixin-zhao commented Oct 16, 2021

Thank you! It works perfectly

Copy link

jonz94 commented Jun 27, 2022

Add another option for Linux user:
It's a key remapping daemon for Linux, and it's really neat!

Copy link

salahmak commented Jun 29, 2022

is there an alternative for this on wayland?

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