Swapping CapsLock and Ctrl in Linux

75 阅读1分钟

When we are using vi (or alternatives such as vim, nvim, helix editor), we frequently hope to Swap the CapsLock and Ctrl keys in a regular keyboard, just like what HHKB's layout. This is actually possible with a few command line strikes to make it work for the current login, or a few configurations to make it take effect forever.

For current login

setxkbmap -option "ctrl:nocaps"

This setting means that Swap Ctrl and Caps Lock.

Then everything will work as expected. If you'd like to inspect current options, simply type:

setxkbmap -query

which outputs something like:

rules:      evdev
model:      pc105
layout:     us
options:    ctrl:swapcaps

For more details, see man xkeyboard-config.

For all later logins

If we would like to make the swap work permanently, one quick solution it to execute setxkbmap -option "ctrl:nocaps" in the profile file. Yes, ~/.profile will be a direct way to put the command for the current user, if you are using Bash as the login shell; for sure, /etc/profile will affect all the users with Bash as the login shell.

Another possibility is to use the same option in the file /etc/default/keyboard, which might end up looking like this:

# KEYBOARD CONFIGURATION FILE

# Consult the keyboard(5) manual page.

XKBMODEL="pc105"
XKBLAYOUT="us"
XKBVARIANT=""
# XKBOPTIONS=""
XKBOPTIONS="ctrl:swapcaps"

BACKSPACE="guess"

Apparantly, the settings in this file affects all the users. If we would like to only affect the current user, simply use ~/.keyboard to overwrite the settings. Note that directly modifying the variables in the configuration files might require restarting or re-login or reconfigure to take effect.

References

  1. opensource.com/article/18/…
  2. wiki.archlinux.org/title/Xorg/…