How do I swap Alt and Windows keys with xmodmap?
I have a Mac keyboard where the Alt/Win (i.e. Option/Command) keys are inverted compared to a regular PC keyboard, and I'd like to swap them. I haven't had any luck with xmodmap so far. The standard configuration is as follows:
shift Shift_L (0x32), Shift_R (0x3e)
lock Caps_Lock (0x42)
control Control_L (0x25), Control_R (0x69)
mod1 Alt_L (0x40), Alt_R (0x6c), Meta_L (0xcd)
mod2 Num_Lock (0x4d)
mod3
mod4 Super_L (0x85), Super_R (0x86), Super_L (0xce), Hyper_L (0xcf)
mod5 ISO_Level3_Shift (0x5c), Mode_switch (0xcb)
Currently, the left/right alt keys are keycodes 64 and 108, while the left/right windows keys are 133 and 134.
I appreciate any help!
Solution 1:
This is my Xmodmaprc:
! Exchange left Alt and left Win
remove mod1 = Alt_L
remove mod4 = Super_L
add mod1 = Super_L
add mod4 = Alt_L
! Exchange tilde and lesser/greater
keycode 94 = grave asciitilde dead_grave dead_tilde grave asciitilde
keycode 49 = less greater less greater bar brokenbar bar brokenbar
! Steve, your keyboard sucks
You'll have to modify your .xsession or .xinitrc to call xmodmap .Xmodmaprc
Solution 2:
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.
Solution 3:
I found that on Ubuntu 16.04 that uses xkb and gnome you need a few extra steps when compared to Christopher Devine's answer answer above if you want to retain all of the short cut behavior for the alt and super keys. This was the easiest solution I found.
To get your keycode numbers, on the command line $ xev
and type the keys you want to get the number for. For more info on that see this post: xev and xmodmap usage
Doing so gives me:
Super_L
has keycode 133
Alt_L
has keycode 64
So to swap the Windows aka Command (on Mac) aka Super key with the left Alt key I do:
remove mod1 = Alt_L
remove mod4 = Super_L
keycode 133 = Alt_L
keycode 64 = Super_L
add mod1 = Alt_L
add mod4 = Super_L
The last two lines add the mod1 and mod4 back into the keysym for Alt_L and Super_L just as they were removed. If this isn't done, I loose the shortcuts associated with the super key (search) and it also breaks the alt+tab combo to switch between windows.
You can add these commands into a file like .xmodmap and then
$ xmodmap .xmodmap
will run them.
Alternatively, you can run these sequentially on the command line like:
$ xmodmap -e <expression here>
If you want to return to the default key mappings, then you can reverse the numbers in the code above, like:
remove mod1 = Alt_L
remove mod4 = Super_L
keycode 64 = Alt_L
keycode 133 = Super_L
add mod1 = Alt_L
add mod4 = Super_L
or in Ubuntu I just run:
$ setxkbmap