9

I want to use my Apple long aluminium keyboard with swapped Command and 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.

2
  • 1
    This solution did not work for me. It is most likely outdated (actually I'm pretty sure, just can't find the reference now). I did try it before posting. That's why I specified the Ubuntu version. Apr 14, 2015 at 15:49
  • 1
    @WaldirLeoncio I hope this makes it clear why the question is NOT a duplicate: askubuntu.com/questions/325272/… Apr 15, 2015 at 10:24

2 Answers 2

8

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.

4
  • 2
    Should be !option = symbols section, not options = symbols (spent time searching for "options =" to find the section).
    – Ben Davis
    Apr 27, 2017 at 19:46
  • See also here: reddit.com/r/Ubuntu/comments/8lnr8q/remapping_keys Oct 27, 2018 at 11:09
  • Like most things with xkb and xmodmap, doesn't work, probably broke my system in other way, I'm sick of this crap and I really wish linux would get its crap together with the most basic freaking thing about key mapping and binding.
    – Seph
    Sep 20, 2019 at 10:22
  • Do mind this was written in 2014 and doesn’t apply to modern distributions. Sep 22, 2019 at 8:40
4

In 16.04, here's the way I finally got this to work. Xmodmap doesn't work universally in all apps, gnome tweak tool lacked the function, dconf editing a custom altwin2 key swap (like the main answer here) failed, so I was tearing my hair out until I combined several answers into this complete, simple, and elegant solution:

gksudo gedit /usr/share/X11/xkb/symbols/pc

change it to:

default  partial alphanumeric_keys modifier_keys
xkb_symbols "pc105" {

key <ESC>  {    [ Escape        ]   };

// The extra key on many European keyboards:
key <LSGT> {    [ less, greater, bar, brokenbar ] };

// The following keys are common to all layouts.
key <BKSL> {    [ backslash,    bar ]   };
key <SPCE> {    [    space      ]   };

include "srvr_ctrl(fkey2vt)"
include "pc(editing)"
include "keypad(x11)"

key <BKSP> {    [ BackSpace, BackSpace  ]   };

key  <TAB> {    [ Tab,  ISO_Left_Tab    ]   };
key <RTRN> {    [ Return        ]   };

key <CAPS> {    [ Caps_Lock     ]   };
key <NMLK> {    [ Num_Lock      ]   };

key <LFSH> {    [ Shift_L       ]   };
key <LCTL> {    [ Alt_L     ]   };
key <LWIN> {    [ Super_L       ]   };

key <RTSH> {    [ Shift_R       ]   };
key <RCTL> {    [ Alt_R     ]   };
key <RWIN> {    [ Super_R       ]   };
key <MENU> {    [ Menu          ]   };

// Beginning of modifier mappings.
modifier_map Shift  { Shift_L, Shift_R };
modifier_map Lock   { Caps_Lock };
modifier_map Control{ Control_L, Control_R };
modifier_map Mod2   { Num_Lock };
modifier_map Mod4   { Super_L, Super_R };

// Fake keys for virtual<->real modifiers mapping:
key <LVL3> {    [ ISO_Level3_Shift  ]   };
key <MDSW> {    [ Mode_switch       ]   };
modifier_map Mod5   { <LVL3>, <MDSW> };

key <ALT>  {    [ NoSymbol, Control_L, Control_R    ]   };
//include "altwin(meta_alt)"
key <LALT> {    [ Control_L     ]   };
key <RALT> {    [ Control_R     ]   };
modifier_map Mod1   { Alt_L, Alt_R, Meta_L, Meta_R };

key <META> {    [ NoSymbol, Meta_L, Meta_R  ]   };
modifier_map Mod1   { <META> };

key <SUPR> {    [ NoSymbol, Super_L ]   };
modifier_map Mod4   { <SUPR> };

key <HYPR> {    [ NoSymbol, Hyper_L ]   };
modifier_map Mod4   { <HYPR> };
// End of modifier mappings.

key <OUTP> { [ XF86Display ] };
key <KITG> { [ XF86KbdLightOnOff ] };
key <KIDN> { [ XF86KbdBrightnessDown ] };
key <KIUP> { [ XF86KbdBrightnessUp ] };
};

hidden partial alphanumeric_keys
xkb_symbols "editing" {
key <PRSC> {
type= "PC_ALT_LEVEL2",
symbols[Group1]= [ Print, Sys_Req ]
};
key <SCLK> {    [  Scroll_Lock      ]   };
key <PAUS> {
type= "PC_CONTROL_LEVEL2",
symbols[Group1]= [ Pause, Break ]
};
key  <INS> {    [  Insert       ]   };
key <HOME> {    [  Home         ]   };
key <PGUP> {    [  Prior        ]   };
key <DELE> {    [  Delete       ]   };
key  <END> {    [  End          ]   };
key <PGDN> {    [  Next         ]   };

key   <UP> {    [  Up           ]   };
key <LEFT> {    [  Left         ]   };
key <DOWN> {    [  Down         ]   };
key <RGHT> {    [  Right        ]   };
};

Save.

rm -rf /var/lib/xkb/*

(I don't know if this is required, but I did it.)

Reboot.

6
  • Thanks for that. I will test it when I migrate to 16.04 in the near future. Oct 12, 2016 at 8:47
  • Just used this on my 16.10 upgrade. Worked very nicely.
    – Tom Mercer
    Oct 21, 2016 at 5:49
  • 1
    By far the simplest answer, worked like a charm on my Fedora. Thanks!
    – hakunin
    Feb 16, 2020 at 22:06
  • Works like a charm on a Manjaro 20. Tks a lot. Nov 22, 2020 at 16:04
  • It would be good to get some explanation of what's going on, what the syntax means, and if this is being applied on top of or is replacing the default set up, how to remove it, which path names and identifiers are mandatory and which are arbitrary, and how the different files in /usr/share/X11/xkb work together.
    – NeilG
    Oct 17, 2021 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.