Remap Keys in Linux

32,671

Solution 1

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.

Solution 2

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

Solution 3

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
    

Solution 4

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!

Share:
32,671

Related videos on Youtube

Nate
Author by

Nate

Updated on September 17, 2022

Comments

  • Nate
    Nate almost 2 years

    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.

  • Nikhil
    Nikhil almost 15 years
    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.
  • mas
    mas almost 15 years
    @Nikhil Chelliah - showkey and setkeycodes are system level so you'd need to sudo them.
  • Samuel Lampa
    Samuel Lampa almost 11 years
    What is that "ISO_Prev_Group ISO_Prev_Group NoSymbol ISO_Prev_Group" part?