17

I'd like to switch my Ctrl and Alt keys under Linux (to make it more Mac-like). How do I do this?

If it helps, I'm running Ubuntu 9.04.

5 Answers 5

14

First off, try looking in Preferences > Keyboard > Layouts > Layout Options under the Alt/Win key behavior if there is a suitable option there for you.

If that doesn't do it for you, these xmodmap commands would switch left Ctrl with left Alt (at least with my keyboard).

xmodmap -e 'keycode 37 = Alt_L  ISO_Prev_Group ISO_Prev_Group NoSymbol ISO_Prev_Group' 
xmodmap -e 'keycode 64 = Control_L' 

To get your keycode, you can run the program xev in a terminal window and press first Ctrl and note the keycode, then Alt to get the keycode.

1
  • 9
    What is that "ISO_Prev_Group ISO_Prev_Group NoSymbol ISO_Prev_Group" part? Jul 24, 2013 at 19:34
9

xmodmap - utility for modifying keymaps and pointer button mappings in X

showkey - examine the codes sent by the keyboard

setkeycodes - load kernel scancode-to-keycode mapping table entries

3
  • 3
    Running showkey produces the error, "Couldnt get a file descriptor referring to the console." Looks like this has happened to a few others on the net...hope it's just my system and not (K)ubuntu's setup.
    – Nikhil
    Jul 15, 2009 at 15:16
  • 2
    @Nikhil Chelliah - showkey and setkeycodes are system level so you'd need to sudo them.
    – mas
    Jul 15, 2009 at 15:56
  • 1
    Note that the keycodes showkey reports are offset by 8 w.r.t what xmodmap expects (see unix.stackexchange.com/questions/120589/…). People might want to use xev instead, which is consistent with xmodmap
    – Alex
    Dec 8, 2022 at 10:41
6

Do the following steps:

  1. Create a new file. Let's call it remap.txt.

  2. Add the following text to the file:

    !
    ! Swap Alt_L and Control_L
    !
    remove mod1 = Alt_L
    remove Control = Control_L
    keysym Control_L = Alt_L
    keysym Alt_L = Control_L
    add mod1 = Alt_L
    add Control = Control_L
    

    The above is like a script for xmodmap. It will change the current key bindings.

  3. Run the following commands to run the script:

    # xmodmap remap.txt
    
  4. (optional) To load the same settings after reboot run:

    # sudo dumpkeys > ~/.Xmodmap
    
3

I am using Ubuntu 20.03.2 LTS, and for some reason simply re-attributing a given Keysym (Control_L or Alt_L) to a given KeyCode (37 and 64) using xmodmap (as described above as well as here) doesn't work out of the box anymore.

Now you need to first remove the Keysyms from their respective "modifiers", before swapping the Keysym-KeyCode mapping, before adding them back to ensure that they are taken into account across applications (I found the trick thanks to this forum):

1 - Find out which keys belong to which modifiers

xmodmap # Control_L should be in control and Alt_L in mod1

2 - Remove keys from their respective modifiers

xmodmap -e "remove control = Control_L"
xmodmap -e "remove mod1 = Alt_L"

3 - Find out the Keysym-KeyCode mapping on your machine

xmodmap -pk # For me, Control_L is mapped to 37 and Alt_L to 64

4 - Swap the Keysym-KeyCode mapping

xmodmap -e "keycode 37 = Alt_L"
xmodmap -e "keycode 64 = Control_L"

5 - Check with xev that the remapping is correct

xev # Then press left ctrl and alt key to witness that they have been swapped in the terminal readout

6 - Add keys back to their respective modifiers

xmodmap -e "add control = Control_L"
xmodmap -e "add mod1 = Alt_L"

And that't it!

7 - Of course, these changes are not permanent - now that you know that they work, create “swap.desktop” file and put it into ~/.config/autostart with following contents in it:

[Desktop Entry]
Name=Swap
Exec=xmodmap -e "remove control = Control_L" && xmodmap -e "remove mod1 = Alt_L" && xmodmap -e "keycode 37 = Alt_L" && xmodmap -e "keycode 64 = Control_L" && xmodmap -e "add control = Control_L" && xmodmap -e "add mod1 = Alt_L"
Terminal=false
Type=Application

8 - Now make it executable

chmod +x ~/.config/autostart/swap.desktop

And here you go!

2

A newer (albeit beta at time of writing) option is using a tool like kbct: https://github.com/samvel1024/kbct .

This allows for having a yaml-style configuration and is intended to be more agnostic to whether one is using Wayland or X11. Found this after looking through previous answers and realizing they may not work under Wayland.

Another popular (and likely more stable) one is kmonad: https://github.com/kmonad/kmonad

1

You must log in to answer this question.

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