29

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.

2 Answers 2

44

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.

10
  • 1
    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.
    – day
    Feb 21, 2013 at 14:44
  • mmn, maybe IBus does itself some keyboard redefinitions. try xkbcomp $DISPLAY some.dump after and before calling IBus, and do a diff of the two dumps (those are complete keyboard definition); you will probably see differences. Feb 22, 2013 at 10:09
  • Hmm, it does, how annoying. Any idea to get rid of the problem? Notice that the command setxkbmap -option caps:escape works. IBus remembered the swapped keys Esc and Caps_Lock keys But it does not do for the swapped Left Shift and Left Control keys.
    – day
    Feb 22, 2013 at 17:14
  • 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) Feb 24, 2013 at 7:11
  • 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!!! Mar 29, 2016 at 15:56
11

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .