I achieved this in Ubuntu 13.04 by combining Christophe's answer (which helped me, but didn't work for the modifiers and I don't need grave and tilde to be dead keys), another answer about setxkbmap, playing with different options in settings-keyboard-layout-options (even looking at the source code) and inspecting the current configuration using the command setxkbmap -query
(which you should also run, to check your specific keyboard model and layout codes as they might differ).
I have one script which I call when I switch to an Apple keyboard (the crucial option being altwin:swap_lalt_lwin
to swap Alt and Super modifier keys):
# clear all options
setxkbmap -model "pc105" -layout "hr,us" -option ""
# set the Apple keyboard
setxkbmap -rules "evdev" -model "pc105" -layout "hr,us" -option "terminate:ctrl_alt_bksp,lv3:rwin_switch,grp:shifts_toggle,caps:ctrl_modifier,altwin:swap_lalt_lwin"
# swap the grave-tilde with less-greater key
# - variant without dead keys
xmodmap -e "keycode 94 = grave asciitilde"
xmodmap -e "keycode 49 = less greater less greater bar brokenbar bar brokenbar"
And another one which I call when I'm back on a normal keyboard:
# clear settings
setxkbmap -model "pc105" -layout "hr,us" -option ""
# pc keyobard
setxkbmap -rules "evdev" -model "pc105" -layout "hr,us" -option "terminate:ctrl_alt_bksp,lv3:rwin_switch,grp:shifts_toggle,caps:ctrl_modifier"
Note that I added the settings
terminate:ctrl_alt_bksp,lv3:rwin_switch,grp:shifts_toggle,caps:ctrl_modifier
to enable ctrl+alt+backspace Gnome session termination, level 3 key access using the right win key, layout shifting by pressing both shift keys and turned my Caps Lock into an additional Ctrl (respectively). You might want to exclude some or all of them.
You can use AutoKey to bind the call to some keyboard shortcut, or call it from your .profile
or .bashrc
if you use the Apple keyboard all the time.
setxkbmap -device 8 dvorak -option altwin:swap_alt_win,ctrl:nocaps
. I only want the right Alt and Win swapped. This does not "cancel out" the left swap:setxkbmap -device 8 dvorak -option altwin:swap_lalt_lwin,altwin:swap_alt_win,ctrl:nocaps
. There is apparently noaltwin:swap_ralt_rwin
option, according toman xkeyboard-config
.