Use setxkbmap to swap the Left Shift and Left Control

23,428

Solution 1

xmodmap is obsolete; so indeed it should be done with the xkb tools.

The swap you want seems not to be included by default with X11 files; so you have to write it yourself.

The page https://web.archive.org/web/20170825051821/http://madduck.net/docs/extending-xkb/ helped me to understand and find a way to do it.

Create a file ~/.xkb/keymap/mykbd where you put the output of setxkbmap, it will be your base keyboard definition; eg:

setxkbmap -print > ~/.xkb/keymap/mykbd

then, create a symbols file to define your key swapping, put it for example in ~/.xkb/symbols/myswap

there, put the following lines:

partial modifier_keys
xkb_symbols "swap_l_shift_ctrl" {
    replace key <LCTL>  { [ Shift_L ] };
    replace key <LFSH> { [ Control_L ] };
};

then, edit the ~/.xkb/keymap/mykbd file, and change the xkb_symbols line to add

+myswap(swap_l_shift_ctrl)

finally, you can load it with xkbcomp -I$HOME/.xkb ~/.xkb/keymap/mykbd $DISPLAY (you cannot use "~" for the -I parameter).

It will probably spit a lot of warnings about undefined symbols for some rare keys, but you can ignore them (eg, redirect error to dave: 2> /dev/null).

If you want to be able to easily swap between a normal and your inverted ctrl/shift one; just create under ~/.xkb/keymap/ another file, without the extra "myswap" option, and load it with xkbcomp. You can make two small scripts to load them.

Solution 2

I ran into some issues mapping LALT to Control_L using Pablo's solution, it appears you need to set the modifier_map options as well.

I copied a template from /usr/share/X11/xkb/symbols/altwin - look for xkb_symbols "ctrl_alt_win" - and modified it to set the following changes:

  • LALT key to Control**
  • LWIN key to Alt
  • MENU key to Windows key

The final ~/.xkb/symbols/myswap file was:

partial modifier_keys
xkb_symbols "swap" {
   key <MENU> {     [   Super_L     ]   };
   key <LALT> { [ Control_L, Control_L  ] };
   key <RALT> { type[Group1] = "TWO_LEVEL",
                symbols[Group1] = [ Alt_R, Meta_R ] };
   key <LWIN> { [ Alt_L, Meta_L ] };
   key <LCTL> { [ Control_L ] };
   key <RCTL> { [ Control_R ] };
   modifier_map Control { <LALT>, <LCTL>, <RCTL> };
   modifier_map Mod1 { <LWIN>, <RALT> };
   modifier_map Mod4 { <MENU> };
};

I also added -w0 to the xkbcomp command to avoid any warning output when opening a new shell.

xkbcomp -w0 -I$HOME/.xkb $HOME/.xkb/keymap/mykbd $DISPLAY

**Switching between Mac and Linux a lot, I like to have two control keys on Linux to approximate the Mac keyboard for my muscle memory.

Share:
23,428
day
Author by

day

Updated on September 18, 2022

Comments

  • day
    day over 1 year

    Is it possible to swap the Left Shift and the Left CTRL keys using setxkbmap instead of xmodmap?

    EDIT

    I have switched to Fcitx, which works way much better with my keyboard layout and customized keymap than IBus in every respect. I highly recommend it.

  • day
    day about 11 years
    Thanks. It does work, but for once. The problem is that I am using an input engine called IBus. Whenever I switched the input method and then back by the key stroke "Ctrl + Space", Left Shift and Left Control are switched back. Any suggestion on how to avoid it.
  • Pablo Saratxaga
    Pablo Saratxaga about 11 years
    maybe IBus read the keyboard when launched. Try unloading IBus, changing your keyboard, and loading it again. If problem persists, then an IBus specific question should be made (and maybe report a bug; imho it is, IBus should detect keyboard changes and honour them)
  • Neil Girardi
    Neil Girardi about 8 years
    Hi. Can anyone tell me how I would use the same technique to swap left Alt key with left Control key? I tried changing the myswap file to: partial modifier_keys xkb_symbols "swap_l_shift_ctrl" { replace key <LCTL> { [ Alt_L ] }; replace key <LALT> { [ Control_L ] }; }; but it didn't work. TIA!!!
  • Pablo Saratxaga
    Pablo Saratxaga about 8 years
    @NeilGirardi: It worked for me. What reports the "xev" command? (you launch it then press the keys, and see what they are sending).
  • Pablo Saratxaga
    Pablo Saratxaga about 8 years
    If your "not working" means some application doesn't behave as expected, keep in mind that the original behaviour of "Alt" key is not just to send Alt_L, but also Meta_L; also maybe there is some modification of the "modifiers" sections... what is the result of your "setxkbmap -print" ?
  • phil294
    phil294 over 7 years
    thanks Pablo. And for anybody wondering, the keyboard keys can be found here charvolant.org/~doug/xkb/html/node5.html
  • Gauthier
    Gauthier over 6 years
    I love the user local keymap, no need to tough system files. However I don't know how to let setxkbmap select the layout you just create in your ~/.xkb. Do you?
  • Aspiring Dev
    Aspiring Dev almost 3 years
    All of this is well and good but the changes get lost if you unplug your keyboard.