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

Artur Meinild
  • 26,018
kalaracey
  • 2,127

9 Answers9

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.

kalaracey
  • 2,127
  • 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 01 '15 at 15:45
  • how do I keep the right hand side win button working? – arcanine Jun 24 '16 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 01 '17 at 06: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 '19 at 23:16
  • https://gist.github.com/kurozael/9a975b5c8a84966dfbfa67d7187d12cf here is the best explanation (based on this answer) – VanPiro Mar 27 '23 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!

clodal
  • 681
  • 5
  • 3
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

Seth
  • 58,122
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.

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)"

fossfreedom
  • 172,746
TenorB
  • 560
  • Worked for me in 12.04. I can't actually believe it, but it worked. After hours of messing with .xmodmap – jyoseph Jan 04 '13 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 '14 at 14:16
  • 14.04 doesn't seem to have that "Options" link. – Waldir Leoncio Apr 14 '15 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)" – Serj Zaharchenko Apr 08 '16 at 09:18
  • 2
    Cant find any of that on Ubuntu 16.04 – Houman Apr 30 '16 at 18:45
  • Just a note - this doesn't swap the keys, it just makes the command keys additional control keys. – Sam Nov 13 '17 at 01:10
  • Under Linux Mint (based off Ubuntu), I found that the following maps more closely: https://unix.stackexchange.com/a/743201/569254 – Paulo Apr 17 '23 at 18:11
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
muru
  • 197,895
  • 55
  • 485
  • 740
RasmusWL
  • 196
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".
Earl Ruby
  • 735
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.

Emilio
  • 321
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.

Tom Mercer
  • 1,658
  • 3
  • 17
  • 36
  • 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? – Pierre Thibault Jan 14 '18 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 '18 at 02: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.

    – Pierre Thibault Jan 15 '18 at 06: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 '18 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. – Pierre Thibault Jan 15 '18 at 22:46
  • I tried your file. But I want to make my right Alt to be a left Ctrl. I am unable to do that. It stays a ISO_Level3_Shift. It is the key that I am unable to change. There is something special about that key. – Pierre Thibault Jan 15 '18 at 23:41
  • Ok, are you crossing left alt into right ctrl also?

    key { [ Alt_L ] }; Needs to change to key { [ Alt_R ] };

    and vice versa.

    Also, what are you making left control into?

    I bet the problems are arising from incomplete/duplicate mapping pairs. I know you want to make Ralt into Lctrl, but what do you want to make Lctrl into? Ralt? Are any other keys changing mappings? Please specify all the key map changes.

    – Tom Mercer Jan 16 '18 at 00:46
  • What I want is:

    left ctrl = alt right left win = alt left left alt = left ctrl right ctrl = right ctrl right win = alt left right alt = left ctrl caps lock = win left

    Everything is fine except that alt right stays as it is. I have { [ Ctrl_L ] } but this is not working. Changing the keyboard layout does not help either. Are you able to change alt right into a ctrl left?

    – Pierre Thibault Jan 16 '18 at 12:09
  • My best guess is need to change the line to this: modifier_map Mod1 { Alt_R, Control_R, Meta_L, Meta_R };

    If that doesn't solve the alt right => ctrl left problem, post the complete file text somewhere like pastebin so I can see all the changes at once.

    – Tom Mercer Jan 16 '18 at 20:35
  • The solution you proposed crashed my keyboard: I was no longer able to type. Here my actual configuration not working with AltR as CtrlL https://pastebin.com/707VwsZK. Hope you can find something. Thank you. – Pierre Thibault Jan 17 '18 at 02:16
  • That was definitely incomplete. I started from stock file, and only swapped Control_L and Alt_R. Here: https://pastebin.com/0B05v8dW
    If this doesn't work, try deleting this line: " modifier_map Mod1 { Alt_L, Control_L, Meta_L, Meta_R };" and try again. I'm not sure if you will need that line. It should be straightforward to make your other substitutions from there, but I would try to make complete substitutions, whereas you left out caps_lock in your previous mapping scheme. Capslock was to be win left, but nothing was to map to capslock.
    – Tom Mercer Jan 17 '18 at 04:59
  • It made my keyboard crash. It does so even if I remove the line you mentioned. – Pierre Thibault Jan 17 '18 at 09:38
  • Sorry, I don't know why that might be. Are the characters funny (like UTF-8/unicode/whatever) when you copy/paste? Maybe try typing those changes into your own file? It should be possible. – Tom Mercer Jan 18 '18 at 08:32
  • I did put back the pc file like it was before. I now use a completely different solution: I run a bash script using xmodmap to modify the keyboard the way I want it like kalaracey proposed in the beginning of the thread. This new solution works perfectly for me. Thank you Tom for trying. :-) – Pierre Thibault Jan 18 '18 at 13:44