Ben Chuanlong Du's Blog

And let it direct your passion with reason.

Map Keys in Linux

Note: For Vim users, it is appealing to make the Caps Lock function like Escape. It is suggested that you make Caps Lock an additonal Escape rather than swap them. This avoid inconsistent key mapping issues when you work remotely via VNC, NoMachine, etc.

Desktop Environment

Some desktop environment (e.g., Cinnamon, GNOME, etc.) let you define behavior of keys.

GNOME

  1. Install gnome-tweaks. You can use the following command on Debian/Ubuntu based Linux distributions.

    wajig install gnome-tweaks
    
  2. Start gnome-tweaks by running the following command.

    gnome-tweaks
    
  3. Click on the "Keyboard & Mouse" button in the left panel and then the "Additional Layout Options" button under the Keyboard section. gnome-tweaks-1

  4. In the prompt dialog, click on the triangle button next to "Caps Lock behavior" and then select the behavior you want for Caps Lock. gnome-tweaks-2

  5. Restart your Linux machine.

Cinnamon

  1. Open System Settings.

  2. Select Keybord.

  3. Keybord layouts

  4. Options (on the right-bottom corner)

  5. Caps Lock key behavior

  6. Choose the behavior you want.

Keyboard Configuration

Keyboard is configured by the file /etc/default/keyboard on Linux. For example, you can find the line that starts with XKBOPTIONS, and add ctrl:nocaps to make Caps Lock an additional Control key or ctrl:swapcaps to swap Caps Lock and Control. Run the following command after updating the file /etc/default/keyboard to make it in effect.

sudo dpkg-reconfigure keyboard-configuration

This is the recommended way if you want to make key swaping persistent. Another advantage it has over GUI configuration and setxkbmap (see the next section) is that the key swaping works in TTY (without X11 or Wayland).

setxkbmap

Swap the Caps and the Escape keys.

setxkbmap -option -option caps:swapescape

Unfortunately, the key mapping partially pass to virtual machines or remote desktops, which is the worst scenario. It is suggested that you turn off the mapping on the Linux host when you work in a VM or a remote desktop.

More References

Comments