51

My OS is Arch Linux amd64, Gnome ENV.

I want to map the Caps Lock key to Esc (escape) in Arch Linux. I run the command:

xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'

It works well, but a moment later, the Caps Lock key works again. And I must run the command again.

I'm pretty sure that this solution worked well maybe a year ago. What's my problem? Can anyone help me to map the Caps Lock key to Escape key forever in my Arch Linux OS?

9 Answers 9

77

Any of the following (in increasing order of complexity):

  1. Use setxkbmap to remap the key (does not require a daemon and is independent of your desktop environment or window manager). Don't forget to add the command before the exec gnome-session (or similar) line in your ~/.xinitrc or ~/.xsession:

     setxkbmap -option caps:escape
    

    setxkbmap can be found in package extra/xorg-setxkbmap.

  2. dconf-editororg.gnome.desktop.input-sources.xkb-options → Add caps:escape to the aforementioned field.

  3. gnome-session-settings → Startup Programs → Add → Name=Remap caps lock to escape, command=setxkbmap -option caps:escape

  4. Create a custom keyboard layout

FYI, I obtained the XKB rule by grepping /usr/share/X11/xkb/rules for caps and esc.

2
  • Thanks for no. 2. I was looking for that info for ages. No I can set this on the command line via gsettings org.gnome.desktop.input-sources xkb-options ['caps:none', 'numpad:pc', 'numpad:mac'] Aug 20, 2016 at 20:53
  • 2
    Regarding no. 1: If you want to swap it rather than have two escape keys, you can run setxkbmap -option caps:swapescape.
    – comfreak
    Jul 31, 2019 at 17:51
11

Xorg.conf

You can achieve this by editing the file /etc/X11/xorg.conf.d/00-keyboard.conf.

Example file:

Section "InputClass"
        Identifier      "system-keyboard"
        MatchIsKeyboard     "on"
        Option          "XkbLayout" "us"
        Option          "XkbModel"  "pc104"
        Option          "XkbOptions" "caps:swapescape"
EndSection  

You can specify multiple XkbOptions, for example caps:swapcaps,terminate:ctrl_alt_bksp for having esc and caps swapped but also allowing the X to be killed with CtrlAlt Backspace. You can find more info about this in man xkeyboard-config.

GUI

You can also use GNOME Tweak Tool (gnome-tweak-tool package). Just click on Typing and then choose whatever you like from the Ctrl position menu (see image below).

enter image description here

1
  • respect for 00-keyboard.conf option
    – daGo
    Jan 11, 2022 at 20:23
5

For use in X, the Arch Wiki Gnome page has instructions for modifying the keyboard with XkbOptions:

Using the dconf-editor, navigate to the key named org.gnome.desktop.input-sources.xkb-options and add desired XkbOptions (e.g. 'caps:swapescape') to the list.

In the console, you can create a custom keymap for the same effect. Create your personal keymap with the requisite changes for CapsLock and Escape at /usr/share/kbd/keymaps/i386/qwerty/yourmap then tar it and include a line in /etc/vconsole.conf to call it:

KEYMAP=yourmap
3

! I don't know why the answers above don't work. Here is a working one for me. In your ~/.Xmodmap

! 66 is the keycode of Caps_Lock
clear    Lock
keycode  66 = Escape 

and then

$ xmodmap ~/.Xmodmap

I'm using Fedora and non-Gnome window manager. HTH

3

You can use xmodmap. Put your ~/.Xmodmap:

remove Lock = Caps_Lock
keysym Escape = Caps_Lock
keysym Caps_Lock = Escape
add Lock = Caps_Lock

(be sure at starting X will use your ~/.Xmodmap)

4
  • When I put this in ~/.Xmodmap, nothing happens :( Nov 20, 2015 at 20:29
  • Put line xmodmap ~/.Xmodmap to your ~/.xinitrc!
    – uzsolt
    Nov 21, 2015 at 7:04
  • When I put this in ~/.Xmodmap and then run xmodmap ~/.Xmodmap I get errors: xmodmap: .Xmodmap:1: bad remove modifier name 'lock=caps_lock', not allowed xmodmap: .Xmodmap:2: bad keysym target key symbol 'Escape=Caps_Lock' xmodmap: .Xmodmap:3: bad keysym target key symbol 'Caps_Lock=Escape' xmodmap: .Xmodmap:4: bad add modifier name 'lock=caps_lock', not allowed xmodmap: 4 errors encountered, aborting
    – comfreak
    Jul 31, 2019 at 17:47
  • You'll need spaces around equal sign. I modified the answer.
    – uzsolt
    Jul 31, 2019 at 18:03
2

The Arch repos now include two relevant interception tools:


caps2esc (tap=esc, hold=ctrl)

  1. Install via pacman (if using another distro, install for your distro or build from source):

    $ sudo pacman -S interception-caps2esc
    
  2. Create /etc/interception/udevmon.yaml and optionally specify the mode. Here, -m 1 specifies "minimal" mode:

    - JOB: intercept -g $DEVNODE | caps2esc -m 1 | uinput -d $DEVNODE
      DEVICE:
        EVENTS:
          EV_KEY: [KEY_CAPSLOCK, KEY_ESC]
    
  3. Enable and start udevmon:

    $ sudo systemctl enable udevmon
    $ sudo systemctl start udevmon
    

dual-function-keys (customizable remappings)

  1. Install via pacman (if using another distro, install for your distro or build from source):

    $ sudo pacman -S interception-dual-function-keys
    
  2. Create /etc/interception/dual-function-keys/my-mappings.yaml, e.g. to remap tap/hold actions for caps and shift:

    MAPPINGS:
      - KEY: KEY_CAPSLOCK
        TAP: KEY_ESC
        HOLD: KEY_BACKSPACE
      - KEY: KEY_LEFTSHIFT
        TAP: KEY_ENTER
        HOLD: KEY_LEFTSHIFT
    
  3. Create /etc/interception/udevmon.yaml:

    - JOB: intercept -g $DEVNODE | dual-function-keys -c /etc/interception/dual-function-keys/my-mappings.yaml | uinput -d $DEVNODE
      DEVICE:
        EVENTS:
          EV_KEY: [KEY_CAPSLOCK, KEY_LEFTSHIFT]
    
  4. Enable and start udevmon:

    $ sudo systemctl enable udevmon
    $ sudo systemctl start udevmon
    
3
  • 1
    can you use this so that instead of ctrl on hold, it does enter or backspace instead? My right pinky is weak, so it would be nice to give it a little break
    – emilBeBri
    Apr 16, 2022 at 8:00
  • @emilBeBri You can do something like that using dual-function-keys (interception-dual-function-keys in Arch), which is a more configurable version of caps2esc. I updated with an example of remapping caps -> tap=esc, hold=backspace and shift -> tap=enter, hold=shift
    – tdy
    Apr 20, 2022 at 17:29
  • Note that not all combinations work, e.g. I couldn't get hold=enter to work although tap=enter worked.
    – tdy
    Apr 20, 2022 at 19:16
1

The file /etc/X11/xorg.conf.d/00-keyboard.conf can also be auto-generated using systemd-localed. Use the following command:

localectl set-x11-keymap us "" "" caps:escape

Or, including some other useful options:

localectl set-x11-keymap us,de "" "" caps:escape,grp:alt_caps_toggle,grp_led:caps,lv3:ralt_switch_multikey,terminate:ctrl_alt_bksp,eurosign:e,rupeesign:4

Quick explanations:

  • CapsLk functions as Esc
  • Toggles between two keyboard layouts us/de with Alt+CapsLk
  • CapsLk LED indicates which layout is active
  • Right Alt (RAlt) is the "multikey" (see Compose Key)
    • RAlt+e generates
    • RAlt+4 generates (similar to how Shift+4 generates $)
  • Ctrl+Alt+Backspace kills X
1

Another great option is https://github.com/maricn/interception-vimproved interception plugin.

It does Esc on tap, CTRL on hold, plus:

  • Return becomes Right CTRL on hold
  • Hold space to change the hjkl keys on the home row to arrow keys, b to backspace, d to delete and more
0

To swap Caps Lock with Escape, you can use the following command:

setxkbmap -option caps:swapescape
1
  • This works under X11 only, not on Wayland Feb 17, 2023 at 13:27

You must log in to answer this question.