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 isxmodmap -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 runxmodmap -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

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).