81

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
  • 31,035
Kyle L
  • 2,177

9 Answers9

89

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.

Kyle L
  • 2,177
59

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!

Brad Turek
  • 139
  • 7
clodal
  • 691
23

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.

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
  • 59,332
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
  • 174,526
TenorB
  • 560
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
  • 207,228
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
  • 841
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
  • 361
2

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,748
  • 4
  • 20
  • 38