13

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!

1
  • 2
    For me, this swaps both pairs of Alt and Win: 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 no altwin:swap_ralt_rwin option, according to man xkeyboard-config. Aug 9, 2017 at 16:39

5 Answers 5

13

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

8

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

1
  • had an issue inside intellij where alt key is no recognize when just swapping keys with mod1 and mod4. But this reset the keycode work beautifully in intellij. Great job
    – Jimubao
    Mar 25, 2020 at 21:02
7

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.

3
  • 3
    A lot of bla bla. If you just want to swap left alt and super key, use this command: setxkbmap -option altwin:swap_lalt_lwin and if you want to switch both left and right alt and super keys, use this command: setxkbmap -option altwin:swap_alt_win.
    – erik
    Jan 26, 2018 at 13:17
  • Good answer. But how do I do exactly that just for right win and right alt (AltGr)? I recently got a unicomp, where right win is next to the spacebar instead of AltGr, as I am used to
    – cessor
    Aug 8, 2018 at 12:16
  • 1
    @erik, just setxkbmap -option altwin:swap_lalt_lwin works fine! But how do you go back then again? Sep 8, 2020 at 6:08
4

You can also set the hid_apple kernel module options to only modify your apple keyboard but not normal PC keyboards. No need to use xkbmap or Xmodmapr. Just create the file /etc/modules.d/hid_apple.conf with the content

options hid_apple iso_layout=0 fnmode=2 swap_opt_cmd=1

fnmode=2 maps the function keys to F1...F12 without the need to press Fn. Omit it if you prefer the media keys. See the Arch Wiki for more.

Permanently on Ubuntu:

echo options hid_apple iso_layout=0 fnmode=2 swap_opt_cmd=1 | sudo tee -a /etc/modprobe.d/hid_apple.conf
sudo update-initramfs -u -k all
2

I'd recommend against using xmodmap in the future due to it not working in Wayland, but that aside setxkbmap or xkbcomp would be the best method to use going forward and my project Kinto uses it.

Kinto will swap the keys you want, but it will also listen for what app you are using, so that you can keep right keymap for terminal apps still so that it matches your experience with being on a mac.

Here are the relevant links, but I will also include the heart of what Kinto does as well - minus custom keymaps and alternating keymap ability, so you have an idea of what Kinto actually does.

https://github.com/rbreaves/kinto

https://medium.com/@benreaves/kinto-a-mac-inspired-keyboard-mapping-for-linux-58f731817c0

https://gist.github.com/rbreaves/f4cf8a991eaeea893999964f5e83eebb

Gist contents

# permanent apple keyboard keyswap
echo "options hid_apple swap_opt_cmd=1" | sudo tee -a /etc/modprobe.d/hid_apple.conf
update-initramfs -u -k all

# Temporary & instant apple keyboard keyswap
echo '1' | sudo tee -a /sys/module/hid_apple/parameters/swap_opt_cmd

# Windows and Mac keyboards - GUI (Physical Alt is Ctrl, Physical Super is Alt, Physical Ctrl is Super)
setxkbmap -option;setxkbmap -option altwin:ctrl_alt_win

# Windows and Mac keyboards - Terminal Apps (Physical Alt is Super, Physical Super is Alt, Physical Ctrl is Ctrl)
setxkbmap -option;setxkbmap -option altwin:swap_alt_win

#
# If you want a systemd service and bash script to help toggle between
# GUI and Terminal applications then look at project Kinto.
# https://github.com/rbreaves/kinto
#
# Note: The above may not work for Chromebooks running Linux, please look
# at project Kinto for that.
#
# If anyone would like to contribute to the project then please do!
#

You must log in to answer this question.

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