How to remap certain keys or devices in Ubuntu?
Tags: ubuntu
Published: 2021-12-01 08:42:31

---
title: How to remap certain keys or devices in Ubuntu?
published: true
date: 2021-11-25 09:48:30 UTC
tags: Ubuntu
canonical_url: https://stackallflow.com/ubuntu/how-to-remap-certain-keys-or-devices-in-ubuntu/
---

<header>
<p><span style="font-size: 1.2em; color: initial;">keyboard</span><span style="font-size: 1.2em; color: initial;">shortcut-keys</span><span style="font-size: 1.2em; color: initial;">xorg</span></p>
</header>

I’m searching for a way to remap certain keys in ubuntu.

i.e.

I’d like to change <kbd>PgUp</kbd> to <kbd>Home</kbd> or <kbd>PgDown</kbd> to <kbd>End</kbd>.

Does a built-in command or a tool exist reassign keys in Ubuntu/GNOME?

#### Accepted Answer

**Notice:** As of 2013, Ubuntu and derivatives no longer use `xmodmap`, but instead use `xkb`. For more information see [this answer](https://askubuntu.com/questions/325272/permanent-xmodmap-in-ubuntu-13-04/347382#347382?newreg=4eb097870a15490ebbe39d78412f9797). The answer below is no longer relevant for current releases.

* * *

For remapping certain keys you need two tools. First [**xev**](http://manpages.ubuntu.com/manpages/trusty/en/man1/xev.1.html) (command-line tool) and second [**xmodmap**](http://manpages.ubuntu.com/manpages/trusty/en/man1/xmodmap.1.html) (also command-line tool). Both should be available in Ubuntu without extra installing them.

1. Start terminal window and run `xev`. Now it’s active and waits for you to press a key. Then press the key whose behaviour you want to change. i.e. <kbd>PgUp</kbd>.
2. `xev` will output some information about the pressed key. The third line is important. It should look similar to:
```
state 0x10, keycode 110 (keysym 0xff55, Prior), same_screen YES,

```

in this example `Prior` is the name of the behaviour the key is assigned to at the moment, the number keycode is the internal id to recognize the key.  
Now do this with another key i.e. <kbd>PgDown</kbd> give this output

```
state 0x10, keycode 115 (keysym 0xff56, Next), same_screen YES,

```

Here again the interesting part for us is `keycode 115` and `Next` – the name of the behaviour.

3. now when you want to swap the two keys use `xmodmap`.
```
 xmodmap -e "keycode 110 = Next"

```

This changes the key with keycode 110 on your keyboard to the action `Next`. It’s pretty simple.

Note that if the key you are mapping should have a different meaning when used with the <kbd>Shift</kbd> key (for example for British keyboard layouts, <kbd>Shift</kbd>+<kbd>2</kbd> gives quotation marks) then you can simply list the secondary command after the first. For example if you want the key with code 53 to map to backslash normally, but to the bar symbol when used with shift, you might do:

```
 xmodmap -e "keycode 53 = backslash bar"

```

**Additional information:** The sequence of these mappings is <kbd>Key</kbd>, <kbd>Shift+Key</kbd>, <kbd>mode_switch+Key</kbd>, <kbd>mode_switch+Shift+Key</kbd>, <kbd>AltGr+Key</kbd>, <kbd>AltGr+Shift+Key</kbd>. To skip a column use `NoSymbol`. Moreover, [here](http://wiki.linuxquestions.org/wiki/List_of_keysyms) is a comprehensive list of all keysyms.

**Note: These change are for the active X session only and will be lost after reboot.  
When you want to save the changes permanently you have to run the following commands after the ones above:**

```
xmodmap -pke >~/.Xmodmap

```

(it creates a file named `.Xmodmap` in your home directory (`~`))

Then you have to create a file named **`.xinitrc`** in your home directory where you put command `xmodmap .Xmodmap` in.

You can now modify `.Xmodmap` and run `xmodmap .Xmodmap` from console to see the changes immediately. The changes in `.Xmodmap` will persist.

source: [Ubuntu Foruns](http://ubuntuforums.org/showpost.php?p=7675138&postcount=2)

## Bonus stuff:

If the key you are remapping has different behavior depending on a state (  
like how the keys in the numeric keyboard depend on NumLock) you simply have to do **`xmodmap -pm`** to get a list of modifiers and then do:

`xmodmap -e "KEYCODE MODIFIER = behaviour behaviour_with_modifier"`

Suppose, for example, that you want to get a period instead of a comma on the numeric keyboard (useful for most programmers), but you want to keep the “delete” behavior when NumLock is off.

```
xmodmap -e "keycode 91 mod2 = KP_Delete period"

```

`mod2`, because `xmodmap -pm` tells us that `mod2` is `Num_Lock`, the other names are obtained by pressing the keys in `xev`.

The post [How to remap certain keys or devices in Ubuntu?](https://stackallflow.com/ubuntu/how-to-remap-certain-keys-or-devices-in-ubuntu/) appeared first on [Stack All Flow](https://stackallflow.com).