Published on

How To Switch Caps Lock with Escape in Linux

Authors
  • avatar
    Name
    ttyS3
    Twitter

Window: X11 DE: GNOME3 OS: ArchLinux

为什么会有这种需求? 当然是 vim 用户才有这种需求。

setxkbmap大法

setxkbmap -option caps:swapescape

To make this work at startup, you can put it in ~/.profile as this will only run after the interactive login.

gnome-tweak大法

对于GNOME用户来说,最简单省事的方法当然是使用 gnome-tweak

通过gnome tweak设置,实际上相当于执行了:

gsettings set org.gnome.desktop.input-sources xkb-options "['caps:swapescape']"

当前值可通过get获取:

❯ gsettings get org.gnome.desktop.input-sources xkb-options
['lv3:ralt_switch', 'caps:swapescape']

xkb rules 大法

新建一 /etc/X11/xorg.conf.d/90-custom-kbd.conf

内容大致如下:

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

xmodmap大法

最古老的方法应该是 xmodmap 大法吧,这也是最容易找到答案的。但是这应该是老灯最不推荐的方法了,因此放到最后。

~/.Xmodmap 内容如下:

clear Lock
keysym Caps_Lock = Escape
keysym Escape = Caps_Lock
add Lock = Caps_Lock

然后开机后执行: xmodmap ~/.Xmodmap

文档上说 GDM 会自动执行这个,不需要手动干预执行,但是老灯测试,实际上 GNOME3.38 并不会执行这个。

see https://bugzilla.redhat.com/show_bug.cgi?id=873656

这个文章 也确认了这一点。也就是说差不多10年前这个就不会自动load了。

October 13, 2010 11:20 pm Sergey Udaltsov GNOME (gnome-settings-daemon) was supporting custom xmodmap files for ages – as a convenient way to tweak the kbd config. I heard several times about people using that feature – even though it was never important, used by minority. Yesterday that feature has gone from g-s-d.

troubleshooting

xkb-options 不工作?

可能你使用的是较老版本的有bug的GNOME, 比如 Ubuntu 20.04 with gnome-shell 3.36.4-1ubuntu1~20.04.2

fixed in 3.38 (see https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/3299#note_977234 )

Refs

https://blogs.gnome.org/sudaltsov/2010/10/13/xmodmap-and-gnome/

https://bugzilla.redhat.com/show_bug.cgi?id=873656

https://gitlab.gnome.org/GNOME/gnome-tweaks/-/issues/50

https://askubuntu.com/questions/363346/how-to-permanently-switch-caps-lock-and-esc

https://wiki.archlinux.org/index.php/xmodmap#Custom_table

https://superuser.com/questions/566871/how-to-map-the-caps-lock-key-to-escape-key-in-arch-linux/1239550#1239550

https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/3299

https://wiki.archlinux.org/index.php/X_keyboard_extension#Using_rules