Ubuntu – How to swap Command and Control keys with xkb step by step

apple-keyboardkeyboard-layoutxkb

I want to use my Apple long aluminium keyboard with swapped Command Cmd and Control Ctrl keys. How is this done step by step on Trusty Tahr (14.04) using xkb?

Note: This solution doesn't work for me as xkb replaced xmodmap in 13.04 or even earlier.

Best Answer

This answer is mostly based on the answer given here. The reason I ask and answer this question again is the final step, which was not fully described. For further reading about xkb look here, here, and here.

  1. Create a file in /usr/share/X11/xkb/symbols (could also be in /etc/X11/xkb/symbols) called altwin2 and containing the following mapping:

    // Control is SWAPPED with Win-keys 
    partial modifier_keys
    xkb_symbols "cmd_n_ctrl" {
        key <LWIN> {        [       Control_L               ]       };
        key <RWIN> {        [       Control_R               ]       };
        key <LCTL> {       [       Super_L         ]       };
        modifier_map Control { <LWIN>, <RWIN> };
        modifier_map Mod4 { <LCTL> };
    };
    
  2. Insert the following line under the option = symbols section in /usr/share/X11/xkb/rules/evdev (disregard the warning on the first line):

    altwin2:cmd_n_ctrl               =       +altwin2(cmd_n_ctrl)
    
  3. Add the new option to /usr/share/X11/xkb/rules/evdev.lst under the section option:

    altwin2:cmd_n_ctrl    Win swapped with Ctrl
    
  4. If you don't know where your keyboard configuration file is, you can edit it using dconf-editor, adding "altwin2:cmd_n_ctrl" in xkb-options under org::gnome::desktop::input-sources as shown here. If you know where your configuration file is, you should include the new option in the XkbOptions field as shown below:

    Section "InputClass"
            Identifier "keyboard-layout"
            Driver "evdev"
            MatchIsKeyboard "yes"
            Option "XkbLayout" "us, ru, ca, fr"
            Option "XkbOptions" "altwin2:cmd_n_ctrl"
    EndSection
    
  5. Either reboot or restart lightdm to update the changes:

    sudo restart lightdm
    

NOTE: if any changes are made directly in the layout files, i.e. not using options, the cached files in /var/lib/xkb/ need to be deleted as indicated here.