Sunday, August 30, 2020

Switching CTRL and ALT keys in Ubuntu 20.04

UPDATE 1: In a more recent distro, items 1-3 might not be needed, since they might already exist in the default xkb config files. Search the files for what we're about to add, and do not add the suggested items if they already exist.

UPDATE 2: If you don't use gnome, like I do on my x-only-no-desktop-environment arch linux setup, item 4 has no effect. Instead you can run "setxkbmap -option ctrl:swap_lalt_lctl -option ctrl:swap_ralt_rctl" (e.g. by putting that in your ~/.xinitrc file).

 Adapted from this: https://askubuntu.com/questions/885045/how-to-swap-ctrl-and-alt-keys-in-ubuntu-16-04

1. Edit /usr/share/X11/xkb/symbols/ctrl and add this snippet at the end:

// Swap the functions of the right Alt key and the right Ctrl key.
partial modifier_keys
xkb_symbols "swap_ralt_rctl" {
    replace key <RALT> { [ Control_R, Control_R ] };
    replace key <RCTL> { [ Alt_R, Meta_R ] };
};

NOTE: The reason we only deal with swapping right alt and right ctrl in this step (and the next ones) is that the functionality for switching left alt and left ctrl is already in place and only needs to be enabled in step 4.

2. Edit /usr/share/X11/xkb/rules/evdev.lst and add the following line to the "! option" section:

ctrl:swap_ralt_rctl Swap Right Alt key with Right Ctrl key

3. Edit /usr/share/X11/xkb/rules/evdev and add the following line to the "! option = symbols" section:

ctrl:swap_ralt_rctl   =   +ctrl(swap_ralt_rctl)

4. Run this:

dconf write "/org/gnome/desktop/input-sources/xkb-options" "['ctrl:swap_lalt_lctl','ctrl:swap_ralt_rctl']"