80

I have scoured the web, and I can't seem to get Xmodmap / anything to work properly. I want to swap BOTH my Command keys with my Control key - in essence, inverting what they do currently.

How would I do that?

I tried xmodmap -e "keycode 133 = Control_L" and corresponding commands for keycodes 134 (right command) and 37 (control left). I even tried these commands, plus xmodmap -pke > ~/.Xmodmap and xmodmap ~/.Xmodmap in .xinitrc.

My end goal is I want HUD / Dash to come up when I click control and command to functional control, i.e., control-v is a commond shortcut to paste, i would click command-v

9 Answers 9

87

I figured it out. Put the following in ~/.Xmodmap:

clear control
clear mod4

keycode 105 =
keycode 206 =

keycode 133 = Control_L NoSymbol Control_L
keycode 134 = Control_R NoSymbol Control_R
keycode 37 = Super_L NoSymbol Super_L

add control = Control_L
add control = Control_R
add mod4 = Super_L

To test, run the command:

xmodmap ~/.Xmodmap

To make it run every time, add it to Startup Applications.

This should work on any MacBook Pro; the issue was that Xmodmap was assigning Super_L to the left and right command keys, and Control_L to the left control key. For some reason, by default, Ubuntu thinks that the MacBook Pro has a standard 105 key keyboard (probably thinking about the full Apple Keyboard) but that is wrong. Anyway, I reassigned the keycodes to the right keysims, then assigned the keysims to control and mod4.

5
  • I have an extra question: how can I change the left Control key to become the alt key, and viceversa, the alt key next to the left Control key to become the left Control key?
    – 719016
    Jun 1, 2015 at 15:45
  • how do I keep the right hand side win button working?
    – arcanine
    Jun 24, 2016 at 12:40
  • Mind that this does not work well with keyboard layout switching. So if you use more than one layout, you'll have to run xmodmap after every layout change. Answer below that suggests editing /usr/share/X11/xkb/symbols/pc is immune to that.
    – clorz
    May 1, 2017 at 6:59
  • Does it runs in on VNCViewer. I am using Gnome desktop and would like to replace my apple keyboard (Command key) with Ctrl Key.
    – ahmed
    Sep 30, 2019 at 23:16
  • gist.github.com/kurozael/9a975b5c8a84966dfbfa67d7187d12cf here is the best explanation (based on this answer)
    – VanPiro
    Mar 27, 2023 at 15:28
58

I just installed Ubuntu 16.04 dual boot on my MBP13" yesterday. For Ubuntu 16.04, here's how i did it:

Step 1: Launch terminal

Step 2: Edit X Keyboard Extension

sudo nano /usr/share/X11/xkb/symbols/pc

Step 3: Make the following changes. Ensure your file looks like this:

key <LCTL> {    [ Super_L       ]   };
key <LWIN> {    [ Control_L     ]   };

...

key <RCTL> {    [ Super_R       ]   };
key <RWIN> {    [ Control_R     ]   };

Step 4: Clear xkb's cache

sudo rm -rf /var/lib/xkb/*

Step 5 (conditional): If the keys are not swapped after step 4, restart your computer. It worked for me after this.

Hope it helps, good luck!

11
  • 5
    This is a much better solution than the accepted answer. Oct 22, 2016 at 15:42
  • 1
    Did not work for me.
    – chovy
    Dec 18, 2016 at 5:24
  • 5
    This is the answer. Needs restarting after change. Thanks Sep 17, 2017 at 8:34
  • 1
    This worked for me! Finally switching between Mac OS and Linux doesn't involve nearly as much muscle memory reprogramming :)
    – JoLoCo
    Jan 4, 2018 at 17:45
  • 5
    Works perfectly, after restart, ubuntu 18.04
    – Marco M
    Jul 6, 2019 at 11:48
22

Go into System -> Preferences -> Keyboard

Click on the "Layouts" tab and then click the "Layout Options" button.

Click on "Alt/Win key behavior"

Select "Control is mapped to Win keys (and the usual ctrl key).

from http://ubuntuforums.org/showthread.php?t=1293081

6
  • 10
    Just a note - this doesn't swap the keys, it just makes the command keys additional control keys. Aug 6, 2013 at 4:08
  • This does not work for Ubuntu 16.04.
    – Unique
    Feb 26, 2018 at 11:32
  • This seems to lead to the desired behavior in Ubuntu 18.04 May 22, 2018 at 20:18
  • I used "alt is swapped with Win" as I'm using Apple keyboard on Ubuntu.
    – SM Adnan
    Oct 3, 2019 at 0:49
  • install Tweaks in Ubuntu, and use that one to update Keyboard setting as mentioned in this answer. Ubuntu's system preferences Keyboard did not show this option for me. Dec 11, 2019 at 19:34
21

Updated for 18.04 since none of the provided answers seemed to work on my system. I did a clean install of 18.04 and attempting to use a wired aluminum apple keyboard. Default behavior of Left Super on English US is mapped to Gnome Desktop Dashboard. Tweak tool had all the necessary settings in an easy to use GUI!

sudo apt install gnome-tweak-tool

First I swapped the dashboard hotkey to the right side and then under "Additional Layout Options" you can use "Ctrl position/ Swap Left Win with Left Ctrl" to good effect.

7
  • gnome-tweak-tool is amazing, thank you! Dec 20, 2018 at 19:58
  • This should be the accepted answer. For the Mac, I'm presently finding that Gnome Tweak Tools has the setting I want under Keybord & Mouse > Additional Layout Options > Ctrl position > Swap Left Win with Left Ctrl. The Mac keyboard's Command key seems to be referred to as "Win".
    – bhaller
    Jul 7, 2019 at 16:56
  • In 2019, in Gnome, this is the easiest solution. Thanks
    – jymbob
    Aug 16, 2019 at 8:41
  • This worked for me in 2020 May 27. Tweak is great! May 27, 2020 at 15:06
  • 1
    However, it does not work in Terminal. How to make the new keybinding also works in terminal?
    – panc
    Sep 4, 2021 at 5:15
6

Go to system settings and then to keyboard layout, then to options, then to Win/Alt key behavior, and then check the "Meta switches Ctrl" and then in the Ctrl Position check "Ctrl switches Meta" and there you have it!

on 12.04.1 you change this by:

First, going to "System Settings" then clicking on "Keyboard" then "Layout Settings" (in the bottom left), then "Options" in the bottom right, then expanding the "Alt/Win key behavior" tab, then selecting "Control is mapped to win Keys (and the usual Ctrl keys)"

7
  • Worked for me in 12.04. I can't actually believe it, but it worked. After hours of messing with .xmodmap
    – jyoseph
    Jan 4, 2013 at 22:55
  • This is great, thanks. I have the newer system (what you describe in the second paragraph). Ctrl is mapped to Win keys makes Cmd behave as Ctrl. Ctrl key position -> Left Ctrl as Meta does the opposite. Also as a side note, I had to go to Startup and Shutdown -> Service Manager -> Startup Services and activate Keyboard Daemon (this was off by default).
    – 0__
    Feb 20, 2014 at 14:16
  • 14.04 doesn't seem to have that "Options" link. Apr 14, 2015 at 11:22
  • As for Ubuntu 15.04, this option is available now at "Input Devices>Keyboard>Advanced tab>Alt/Win key behaviour section>Ctrl is mapped to win Keys (and the usual Ctrl keys)" Apr 8, 2016 at 9:18
  • 2
    Cant find any of that on Ubuntu 16.04
    – Houman
    Apr 30, 2016 at 18:45
6

The community Ubuntu help page for Apple keyboards gives the following steps to permanently make this change: (tested 2017-05-27)

Run the following command to append the configuration line to the file /etc/modprobe.d/hid_apple.conf creating it if necessary:

echo options hid_apple swap_opt_cmd=1 | sudo tee -a /etc/modprobe.d/hid_apple.conf

Trigger copying the configuration into the initramfs bootfile.

sudo update-initramfs -u -k all

Optionally, reboot

sudo reboot

To temporarily test it out, you can run

echo 1 | sudo tee /sys/module/hid_apple/parameters/swap_opt_cmd
3

I use a Deck keyboard with Mac keys hooked to a USB switch to switch my keyboard/mouse/camera/microphone between a number of computers, including Mac laptops and Ubuntu boxes. I just wanted the "Apple/Win" key to act like a ctrl key when I was running Ubuntu, so if I hit Apple-C to copy it would work on Ubuntu.

On Ubuntu 20.04:

  • Install tweaks if you haven't already: sudo apt install gnome-tweak-tool
  • Click Activities.
  • Type "Tweaks", click the "Gnome Tweaks" icon.
  • Select "Keyboard & Mouse"
  • Click "Additional Layout Options"
  • Select "Alt/Win key behavior"
  • Pick the key mapping you want. I just wanted my "Apple/Win" key to do the same thing Ctrl does, so I picked "Ctrl is mapped to Win and the usual Ctrl keys".
2

Another solution which does NOT involve Xmodmap, nor depends on gnome-tweaks is to instruct the module to swap them. It already has the functionality.

To play around with the parameters you can change them live. First check the existing parameters with modinfo hid_apple and then modify them on the fly:

echo 2 >> /sys/module/hid_apple/parameters/fnmode
echo 1 >> /sys/module/hid_apple/parameters/swap_opt_cmd
echo 1 >> /sys/module/hid_apple/parameters/swap_fn_leftctrl

Once you are comfortable with the configuration, you can make them permanent. Create a file like this /etc/modprobe.d/hid_apple.conf and add your parameters like this:

options hid_apple fnmode=2 swap_opt_cmd=1

Then remember to call update-initramfs -k -all (or dracut --regenerate-all --force in Fedora) so the parameters get loaded when the module gets loaded on boot, or they will be ignored.

This option is specially good because the command key can be used as AltGr, which helps you type special characters like áéíóúñ, which is not possible with the alternative fixes.

1

Duplicate of How to swap Command and Control keys with xkb step by step?

In 16.04, here's the way I finally got this to work. Xmodmap doesn't work universally in all apps, gnome tweak tool lacked the function, dconf editing a custom altwin2 key swap (like the main answer here) failed, so I was tearing my hair out until I combined several answers into this complete, simple, and elegant solution:

gksudo gedit /usr/share/X11/xkb/symbols/pc

change it to:

default  partial alphanumeric_keys modifier_keys
xkb_symbols "pc105" {

key <ESC>  {    [ Escape        ]   };

// The extra key on many European keyboards:
key <LSGT> {    [ less, greater, bar, brokenbar ] };

// The following keys are common to all layouts.
key <BKSL> {    [ backslash,    bar ]   };
key <SPCE> {    [    space      ]   };

include "srvr_ctrl(fkey2vt)"
include "pc(editing)"
include "keypad(x11)"

key <BKSP> {    [ BackSpace, BackSpace  ]   };

key  <TAB> {    [ Tab,  ISO_Left_Tab    ]   };
key <RTRN> {    [ Return        ]   };

key <CAPS> {    [ Caps_Lock     ]   };
key <NMLK> {    [ Num_Lock      ]   };

key <LFSH> {    [ Shift_L       ]   };
key <LCTL> {    [ Alt_L     ]   };
key <LWIN> {    [ Super_L       ]   };

key <RTSH> {    [ Shift_R       ]   };
key <RCTL> {    [ Alt_R     ]   };
key <RWIN> {    [ Super_R       ]   };
key <MENU> {    [ Menu          ]   };

// Beginning of modifier mappings.
modifier_map Shift  { Shift_L, Shift_R };
modifier_map Lock   { Caps_Lock };
modifier_map Control{ Control_L, Control_R };
modifier_map Mod2   { Num_Lock };
modifier_map Mod4   { Super_L, Super_R };

// Fake keys for virtual<->real modifiers mapping:
key <LVL3> {    [ ISO_Level3_Shift  ]   };
key <MDSW> {    [ Mode_switch       ]   };
modifier_map Mod5   { <LVL3>, <MDSW> };

key <ALT>  {    [ NoSymbol, Control_L, Control_R    ]   };
//include "altwin(meta_alt)"
key <LALT> {    [ Control_L     ]   };
key <RALT> {    [ Control_R     ]   };
modifier_map Mod1   { Alt_L, Alt_R, Meta_L, Meta_R };

key <META> {    [ NoSymbol, Meta_L, Meta_R  ]   };
modifier_map Mod1   { <META> };

key <SUPR> {    [ NoSymbol, Super_L ]   };
modifier_map Mod4   { <SUPR> };

key <HYPR> {    [ NoSymbol, Hyper_L ]   };
modifier_map Mod4   { <HYPR> };
// End of modifier mappings.

key <OUTP> { [ XF86Display ] };
key <KITG> { [ XF86KbdLightOnOff ] };
key <KIDN> { [ XF86KbdBrightnessDown ] };
key <KIUP> { [ XF86KbdBrightnessUp ] };
};

hidden partial alphanumeric_keys
xkb_symbols "editing" {
key <PRSC> {
type= "PC_ALT_LEVEL2",
symbols[Group1]= [ Print, Sys_Req ]
};
key <SCLK> {    [  Scroll_Lock      ]   };
key <PAUS> {
type= "PC_CONTROL_LEVEL2",
symbols[Group1]= [ Pause, Break ]
};
key  <INS> {    [  Insert       ]   };
key <HOME> {    [  Home         ]   };
key <PGUP> {    [  Prior        ]   };
key <DELE> {    [  Delete       ]   };
key  <END> {    [  End          ]   };
key <PGDN> {    [  Next         ]   };

key   <UP> {    [  Up           ]   };
key <LEFT> {    [  Left         ]   };
key <DOWN> {    [  Down         ]   };
key <RGHT> {    [  Right        ]   };
};

Save.

rm -rf /var/lib/xkb/*

(I don't know if this is required, but I did it.)

Reboot.

14
  • This was nice but I am unable to use the same trick to switch my ALT keys as a left Control key. It seems ALT keys work differently. Do you know how can I do it? Jan 14, 2018 at 15:23
  • The same trick works for anything. You have to switch both the key mappings as well as the modifier_map mappings. You can also jump into gnome tweaks or gconf, i forget which, and there are several toggle settings like "swap ctrl and alt".
    – Tom Mercer
    Jan 15, 2018 at 2:53
  • We cannot do a: key <LALT> { [ Control_L ] }; key <RALT> { [ Control_L ] }; Alt is treated differently. So I used Gnome Tweaks. I was not able to have what I wanted exactly but it is not so bad. Jan 15, 2018 at 6:01
  • I'm not sure I understand your problem. If you just want both alts to behave as controls, the file exactly as above will make both alt keys behave as controls, and both control keys behave as alts. If you can specify what you want each key to be, maybe I can help? Did you try this file? What machine are you using, and which keyboard? French keyboard layout might be giving us trouble.
    – Tom Mercer
    Jan 15, 2018 at 18:55
  • No, I didn't try the file. I can see that later in the file you are making the switch of two alt to two ctrl. I missed that. Thank you for pointing it. I will try it. Jan 15, 2018 at 22:46

You must log in to answer this question.

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