How can I persistently remap keys in Ubuntu 16.04?

I've been using 16.04 for a little bit and it seems to use sddm as its desktop manager. It used to be LightDM and GDM before that. Both its predacessors are documented to load ~/.Xmodmap automatically but I can't find anything explicitly saying sddm does.

Therefore it may be advisable to just force it to load with a new script. You just need to run xmodmap ~/.Xmodmap and you can do that a number of ways:

  • Adding it via the graphical start up tools
  • Editing ~/.xinitrc to run it.
  • Adding a new .desktop config in ~/.config/autostart/

There are probably a few dozen other ways to manage this, essentially doing the same thing.


In my long experience with remapping keys in Ubuntu, the permanent solution is to modify a file called evdev in /usr/share/X11/xkb/keycodes.

Run this command in the command prompt.

sudo gedit /usr/share/X11/xkb/keycodes/evdev

Remember gedit is a Ubuntu text editor so you can use nano or vim instead of gedit.

The file is in the format: ALIAS: CODE. You can swap buttons by interchanging codes of different keys. For example, to swap RCONTROL with RETURN, edit two lines in evdev into this:

<RTRN> = 105;

<RCTL> = 36;

To know the number code for a button run xev on the terminal.


I added a file /etc/X11/Xsession.d/80_xmodmap with these contents:

#!/bin/sh
# Set custom keycodes
#
# This file is sourced by Xsession(5), not executed.
# The "|| true" is to ensure that the Xsession script does not terminate on error

USRMODMAP="$HOME/.Xmodmap"

if [ -x /usr/bin/xmodmap ]; then
        if [ -f "$USRMODMAP" ]; then
                /usr/bin/xmodmap "$USRMODMAP" || true
        fi
fi

This works every time.