How to map the Caps Lock key to Escape key in Arch Linux

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?


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.


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


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

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)


! 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