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

arch linuxkeyboardkeymapxmodmap

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?

Best Answer

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.

Related Question