54

There is no such option in Keyboard Layout Options which is sad.

How can I do it manually?

1

5 Answers 5

67

To do this manually you want to create a file called .Xmodmap in your home directory. You could do this using a command from the terminal

~$ gedit ~/.Xmodmap

That will create the file and open it in gedit. Add the following lines to the file:

clear control
clear mod1
keycode 37 = Alt_L Meta_L
keycode 64 = Control_L
add control = Control_L Control_R
add mod1 = Alt_L Meta_L

Save the file and quit gedit. Next time you login the new keymappings will be active. To have the settings take immediate effect run the following command:

~$ xmodmap ~/.Xmodmap

There's a chance that the keycodes above are different on your computer. If the above doesn't work, try running the xev program from the command line. Xev will generate a new small window in the corner of your screen. If you start pressing keys on your keyboard you’ll get all the key mapping information for that key press.

~$ xev

For example if I press “q” when xev is running I get the following output:

KeyRelease event, serial 33, synthetic NO, window 0x4c00001,
root 0xb6, subw 0x0, time 9127480, (21,-17), root:(22,36),
state 0x0, keycode 24 (keysym 0x71, q), same_screen YES,
XLookupString gives 1 bytes: (71) "q"
XFilterEvent returns: False

keycode 24 is the information that we're after. Press your left Ctrl and left Alt buttons while running xev to get your own respective keycodes to confirm that they are the same as the ones used above.

Hope that helps.

5
  • File doesn't exist in Ubuntu 16.04.
    – MERose
    Jan 10, 2018 at 20:09
  • 1
    This used to work with 16.04 but not anymore on 17.10.
    – Niklas
    Feb 16, 2018 at 14:02
  • 3
    I have to rerun xmodmap ~/.Xmodmap every time I reboot. How can I automate that?
    – Timothy
    Oct 31, 2018 at 14:22
  • askubuntu.com/a/749662/894748 @MwirabuaTim check this
    – VnC
    Oct 3, 2019 at 10:32
  • You can avoid running xmodmap on reboot by using gnome-tweak-tool, see answer below. Oct 19, 2020 at 1:37
35

If you don't want to get into the pain of xmodmap, you can also do it through Gnome settings:

  • Install gnome-tweaks with sudo apt-get install gnome-tweaks
  • Open it
  • Go to Keyboard & Mouse -> Additional Layout Options
  • Change the Ctrl position option to Swap Left Alt with Left Ctrl:

Gnome Tweaks screenshot

You could probably also do this via gsettings, but am not sure of the exact config entry name.

6
  • I am trying to use tweak tool in ubuntu gnome 15.10 and it ignores the "swap left win key with left Control". Any ideas? I am trying to swap L-SUPER but assume they are the same key.
    – Dori
    Feb 23, 2016 at 7:12
  • clutching at straws, but maybe you need to restart, or log out and log back into the window manager?
    – hwjp
    Feb 24, 2016 at 12:20
  • Tried this and no diff...
    – Dori
    Feb 24, 2016 at 17:02
  • 12
    It seems that "Typing" was removed in Ubuntu 17.10. It is now in "Keyboard & Mouse" -> "Additional Layout Options" Oct 31, 2017 at 14:34
  • It's under "Additional Layout Options" now
    – ZiglioUK
    Mar 22, 2018 at 11:38
10

Simply create ~/.Xmodmap using a simple text editor and restart the Xserver.


!
! based on: http://www.emacswiki.org/emacs/SwapControlAltAndCapsLock#toc8
!

!----------------------------------------------------------
! Swap Control and Alt keys, both sides
!----------------------------------------------------------

! First clear all modifiers & control
clear control
clear mod1
clear mod4

! Swap Control_L and Alt_L
keycode  64 = Control_L
keycode  37 = Alt_L Meta_L

! Menu becomes Alt_R Meta_R (AltGr)
keycode 135 = Alt_R Meta_R

! Define Control_R and Alt_R similar to Control_L and Alt_L
keycode 108 = Control_L
keycode 105 = Alt_L Meta_L

! We need to set keycodes first, as some programs (emacs!) read
! the key names and seem to ignore what's below.
add mod1    = Alt_L Alt_R Meta_L Meta_R
add mod4    = Super_L Super_R
add control = Control_L Control_R


!------------------------------------------
! Caps_Lock becomes an additional BackSpace
!------------------------------------------
remove lock = Caps_Lock
keysym Caps_Lock = BackSpace
2
  • This worked like a champ on Crouton Ubuntu Unity on Chromebook. I did have to run xmodmap ~/.Xmodmap to get it to load. I'm not sure if I have to do it on each reboot.
    – swt83
    Jun 21, 2015 at 3:46
  • @swt83 : you may be interested on this: github.com/frgomes/carpalx which demonstrates how to redefine the entire keyboard, if you wish. Mar 16, 2017 at 23:22
1

You can swap them by creating an udev .hwdb file to manually map scan codes to key codes. This method works with Wayland, Xorg, and the virtual console.

TLDR

generic input device(USB keyboards),

echo 'evdev:input:b0003v*p*
 KEYBOARD_KEY_700e0=leftalt
 KEYBOARD_KEY_700e2=leftctrl' |\
sudo tee /etc/udev/hwdb.d/70-keyboard.hwdb &&\
sudo systemd-hwdb update &&\
sudo udevadm trigger

AT keyboards

echo 'evdev:atkbd:*
 KEYBOARD_KEY_1d=leftalt
 KEYBOARD_KEY_38=leftctrl' |\
sudo tee /etc/udev/hwdb.d/70-keyboard.hwdb &&\
sudo systemd-hwdb update &&\
sudo udevadm trigger

Find Scan Code

Run sudo evtest command to find the scan codes. You will be prompted for which device to monitor like below.

Available devices:
...
/dev/input/event8:  ... Keyboard 
...

Select the device event number [0-18]: 

Select the event number of your keyboard to continue, then the prompt will show you the information of the key you typed.

Event: time 1652208036.880554, -------------- SYN_REPORT ------------
Event: time 1652208037.016512, type 4 (EV_MSC), code 4 (MSC_SCAN), value 700e0
Event: time 1652208037.016512, type 1 (EV_KEY), code 56 (KEY_LEFTALT), value 0

The value(700e0 in this case) after MSC_SCAN is the scan code of key you pressed

Write .hwdb File

Create /etc/udev/hwdb.d/70-keyboard.hwdb

evdev:input:b0003v*p*
 KEYBOARD_KEY_<hex scan code>=<key code identifier>

or for AT keyboards

evdev:atkbd:dmi:bvn*:bvr*:bd*:svn*:pn*:pvr*
 KEYBOARD_KEY_<hex scan code>=<key code identifier>
  • The evdev:input:* matchs any generic input devices(also USB keyboards). The evdev:atkbd:dmi:bvn*:bvr*:bd*:svn*:pn*:pvr* matches any AT keyboards.
  • The <hex scan code> should be expressed in hex lowercase.
  • The <key code indentifier> should be expressed in lowercasae indetifier or integer. Key code values can be found in the file /usr/include/linux/input-event-codes.h(see KEY_<KEYCODE> variables) or this online list.

Apply Changes

sudo systemd-hwdb updae && sudo udevadm trigger

Example config

evdev:input:b0003v*p*
 KEYBOARD_KEY_700e0=leftalt
 KEYBOARD_KEY_700e2=leftctrl

AT keyboard

evdev:atkbd:*
 KEYBOARD_KEY_1d=leftalt
 KEYBOARD_KEY_38=leftctrl

See Also

1

Background:

I followed cossovich's answer which is the accepted answer, and it works. However I was facing issues when the solution would stop working randomly and would have to manually run ~$ xmodmap ~/.Xmodmap command in the terminal to get it working again.

Alternate solution

Using https://wiki.gnome.org/Apps/Tweaks we could configure the desired behavior via GUI.

Afer installing Tweaks, go to "Keyboard & Mouse" and click on "Additional Layouts".

enter image description here

It would open a modal. In this modal, expand the section for "Ctrl position". Here you can see various options to modify the control key position. Select the checkbox that says, "Left Ctrl as Alt, Left Ctrl as Win, Left Win as Left Alt".

enter image description here

This gave the desired results for me.

You must log in to answer this question.

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