Ubuntu – How to swap ctrl and alt keys in ubuntu 16.04

keyboard

I have read the following:

1.
How to cleanly swap alt and ctrl keys in xubuntu?

2.
How to swap Command and Control keys with xkb step by step?

Other than the above, I also spent quite some time on google and xkb documentation.

I assume, that many users like me switching from Mac to Ubuntu would like to swap the ctrl and alt keys immediately after finishing the installation.

My purpose to post this question (and answer) here is to give a quick solution instead of spending so much time going through the above threads and docs.

Also, none of the above provide the correct answer. (This is not to say that I don't appreciate the help. The above posts did ultimately help me achieve what I wanted to but the responses were not complete.)

Thread 1 only swaps left ctrl with left alt

Thread 2 swaps ctrl and win even though the question was not about win key

Best Answer

  1. Edit the file /usr/share/X11/xkb/symbols/ctrl to add the following lines to swap Right Alt with Right Ctrl key:

    // 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 ] };
    };
    

    Similarly you can add lines to swap Left Alt with Left Ctrl.

  2. Edit the file /usr/share/X11/xkb/rules/evdev.lst to dd the following line in the ! option section:

    ctrl:swap_ralt_rctl Swap Right Alt key with Right Ctrl key
    
  3. Edit the file /usr/share/X11/xkb/rules/evdev to add the following line in the !option = symbols section:

    ctrl:swap_ralt_rctl = +ctrl(swap_ralt_rctl)
    
  4. Use dconf from dashboard (if necessary install it with apt-get install dconf-tools) and either of the following methods:

    • Open dconf editor from dashboard and navigate to the key /org/gnome/desktop/input-sources/xkb-options and set its value to:

      ['ctrl:swap_lalt_lctl','ctrl:swap_ralt_rctl']
      
    • Use dconf from terminal (probably the command below should work instead of the previous step, but I am not sure about it):

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