Map any key to any other key on Ubuntu

ADVERTISEMENT

The spacebar key on my keyboard is broken(long story), so I remapped it on Windows using KeyTweak app. Here’s how I achieved the same on Ubuntu. The instructions are generalized so that you can remap virtually any key to any other key.

Using xmodmap

Remapping keys can be done by running xmodmap on the terminal. The general format is
xmodmap -e "your commands"

To map any action to a key, you can use either the name or key code of that key. A list of key codes and key names (also called actions) is given at the end of this page.

xmodmap -e "keycode CODE = ACTION"

ADVERTISEMENT

Remap modifier keys

Alt, Windows key, Control etc are modifiers with special functions, so they have to be unmapped as modifiers first, then remapped.

Here’s how to remove Alt as a modifier:
xmodmap -e "remove mod1 = Alt_R"

If you need to unmap some other modifier, run xmodmap -pm to find its name.

To remap the alt key, now run
xmodmap -e "keysym Alt_R = space"

If alt-r has been previously mapped, this may not work, so use the key code directly.
xmodmap -e "keycode 108 = space"

If you need to remap some other key, run xmodmap -pke to find its name or code.

Make key remaps permanent

Update: See “Alternate method” below for a better solution to keeping key maps permanent.

These changes are reset at reboot, so you have to make them permanent. One way to do it is add them to the rc.local file. Another way is to add the commands to a file and run that file at startup, which is not as easy as adding them to rc.local.

Open rc.local file under /etc and just before “exit O”, add whichever mapping commands you used above.

Hit Control-X to close, and you’ll be asked to save. Hit Y and then enter.

Alternate method

Instead of editing rc.local, you can add all remap commands in a file, say keymap.txt, and place it in your home folder (can be anywhere else, adjust the commands with the path).

!!!! Swap Alt_L and Control_L !!!!
remove mod1 = Alt_R
keysym Alt_R = space

To run these commands, run
xmodmap keymap.txt

To make these run on startup automatically, run
sudo dumpkeys > ~/.Xmodmap

Reset xmodmap key mapping

If you did the mapping wrong, run setxkbmap -layout us to reset all mappings.

This command resets the keyboard layout to US, so if you use another layout, change the command accordingly.

Key codes and names

Here is a list of commonly used key names and their key codes.

keycode 0x6c = space
keycode 9 = Escape
keycode 10 = 1 exclam
keycode 11 = 2 quotedbl
keycode 12 = 3 sterling
keycode 13 = 4 dollar
keycode 14 = 5 percent
keycode 15 = 6 asciicircum
keycode 16 = 7 ampersand
keycode 17 = 8 asterisk
keycode 18 = 9 parenleft
keycode 19 = 0 parenright
keycode 20 = minus underscore
keycode 21 = equal plus
keycode 22 = Delete
keycode 23 = Tab
keycode 24 = Q
keycode 25 = W
keycode 26 = E
keycode 27 = R
keycode 28 = T
keycode 29 = Y
keycode 30 = U
keycode 31 = I
keycode 32 = O
keycode 33 = P
keycode 34 = bracketleft braceleft
keycode 35 = bracketright braceright
keycode 36 = Return
keycode 37 = Control_L
keycode 38 = A
keycode 39 = S
keycode 40 = D
keycode 41 = F
keycode 42 = G
keycode 43 = H
keycode 44 = J
keycode 45 = K
keycode 46 = L
keycode 47 = semicolon colon
keycode 48 = apostrophe at
keycode 49 = grave asciitilde
keycode 50 = Shift_L
keycode 51 = numbersign asciitilde
keycode 52 = Z
keycode 53 = X
keycode 54 = C
keycode 55 = V
keycode 56 = B
keycode 57 = N
keycode 58 = M
keycode 59 = comma less
keycode 60 = period greater
keycode 61 = slash question
keycode 62 = Shift_R
keycode 63 = KP_Multiply
keycode 64 = Alt_L
keycode 65 = space
keycode 66 = Caps_Lock
keycode 67 = F1
keycode 68 = F2
keycode 69 = F3
keycode 70 = F4
keycode 71 = F5
keycode 72 = F6
keycode 73 = F7
keycode 74 = F8
keycode 75 = F9
keycode 76 = F10
keycode 77 = Num_Lock
keycode 78 = Scroll_Lock
keycode 79 = Home KP_7 KP_7 Home
keycode 80 = Up KP_8 KP_8 Up
keycode 81 = Prior KP_9 KP_9 Prior
keycode 82 = KP_Subtract
keycode 83 = Left KP_4 KP_4 Left
keycode 84 = Begin KP_5 KP_5 Begin
keycode 85 = Right KP_6 KP_6 Right
keycode 86 = KP_Add
keycode 87 = End KP_1 KP_1 End
keycode 88 = Down KP_2 KP_2 Down
keycode 89 = Next KP_3 KP_3 Next
keycode 90 = Insert KP_0 KP_0 Insert
keycode 91 = Delete KP_Decimal KP_Decimal Delete
keycode 92 = 0x1007ff00
keycode 94 = backslash bar
keycode 95 = F11
keycode 96 = F12
keycode 97 = Home
keycode 98 = Up
keycode 99 = Prior
keycode 100 = Left
keycode 101 = Begin
keycode 102 = Right
keycode 103 = End
keycode 104 = Down
keycode 105 = Next
keycode 106 = Insert
keycode 107 = Delete
keycode 108 = KP_Enter
keycode 109 = Control_R
keycode 110 = Pause
keycode 111 = Print
keycode 112 = KP_Divide
keycode 113 = Mode_switch
keycode 114 = Break