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.

day
  • 465

2 Answers2

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.

slm
  • 369,824
  • 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 '13 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. – Pablo Saratxaga Feb 22 '13 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 '13 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) – Pablo Saratxaga Feb 24 '13 at 07: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 { [ Alt_L ] }; replace key { [ Control_L ] }; }; but it didn't work. TIA!!! – Neil Girardi Mar 29 '16 at 15:56
  • @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 Apr 15 '16 at 10:25
  • 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" ? – Pablo Saratxaga Apr 15 '16 at 10:31
  • thanks Pablo. And for anybody wondering, the keyboard keys can be found here http://www.charvolant.org/~doug/xkb/html/node5.html – phil294 Jan 21 '17 at 23:35
  • 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? – Gauthier Aug 23 '17 at 08:44
  • All of this is well and good but the changes get lost if you unplug your keyboard. – Aspiring Dev Jul 01 '21 at 02:33
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.

Jo-el
  • 211