How to remap keys with xmodmap

Setting your keyboard layout should be one of the first things users do when installing or setting up their Linux system. Every system will come with a slew of preconfigured layouts for various languages or preferences. But what if you want to take it a step further and modify a layout to your liking?

That is where the xmodmap command can help us. This command allows us to remap individual keys on our keyboard, which is useful for achieving a personalized and ideal keyboard layout which is just not provided by default in your Linux distro.

In this tutorial, we will take you through the step by step instructions to get the perfect keyboard layout on Linux by remapping keys with the xmodmap command. xmodmap can also be used to map more functionalities to your keyboard since some keys may not be in use.

In this tutorial you will learn:

  • How to view the currently configure key map
  • How to remap keys with xmodmap
  • How to load remapped keys each session start
Output from the xmodmap command, showing the current mapping of our keys
Output from the xmodmap command, showing the current mapping of our keys
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software xmodmap
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user
DID YOU KNOW?
Some Linux distros or desktop environments may override the settings of xmodmap. In that case, you will need to configure your custom keyboard mappings elsewhere. It is worth a shot to follow the steps below to see if they will work on your system.

How to view current keyboard mapping




Get started by opening a command line terminal and entering the following command to see the currently configured keyboard mappings in xmodmap.

$ xmodmap -pke

SCREENSHOT 1
Output from the xmodmap command, showing the current mapping of our keys

A typical entry for a keyboard mapping will look something like this. This happens to be for the q key.

keycode  24 = q Q q Q

As you can see, there are four different mappings for this key. The order of the settings is how we can tell what each one does. Here is what the order means. Keep in mind that not all need to be used, as is the case with our q key which only uses the first four settings.

  1. Ordinary key press
  2. Key + Shift
  3. Key + Mode_switch
  4. Key + Mode_switch + Shift
  5. Key + ISO_Level3_Shift
  6. Key + Shift + ISO_Level3_Shift
NOTE
In the vast majority of setups, you will only need to concern yourself with the first two settings (ordinary key press and key + Shift).

You may also see a mapping that has NoValue, which is necessary to skip a certain mapping then continue with the next setting.

How to remap keys with xmodmap step by step instructions

  1. Start by making a copy of the current mappings and storing the file in your $HOME directory.
    $ xmodmap -pke > ~/.Xmodmap
    
  2. Now, using nano or your favorite terminal or GUI file editor, open this file to make your desired changes.
    $ nano ~/.Xmodmap
    

    Exit the file and save changes when done.

  3. For the changes to take effect, execute the following command to load the configuration into xmodmap.


    $ xmodmap ~/.Xmodmap
    
  4. For good measure, you should add that line to the bottom of the $HOME/.bashrc file so it gets executed whenever a new terminal or shell is opened.
    $ echo xmodmap ~/.Xmodmap >> ~/.bashrc
    

Closing Thoughts

In this tutorial, we saw how to remap keys with xmodmap on a Linux system. This is a quick and easy way to achieve the perfect layout for your keyboard, and customize it to your liking. It also works well as a way to play a trick on your colleagues, but you didn’t hear that from us.



Comments and Discussions
Linux Forum