Umlauts on US Keyboard Layouts on Ubuntu with XKB

Xmodmap had been a reliable tool to bring umlauts to the US keyboard layout. However, on modern Ubuntu systems, xmodmap can result in several problems and does no longer work reliably.

Using XKB, it is possible to define own layouts that work more reliably. Being able to activate them, seems to change amongst Ubuntu version. Several recommendations on how to create and activate custom XKB files did not work for me. One way to make it work for me is described below:

Create a custom XKB file in /usr/share/X11/xkb/symbols/usde as follows:

default partial alphanumeric_keys
xkb_symbols "alt-intl" {
   include "us(basic)"

   key <AD03> { [     e,          E,          EuroSign               ] };
   key <AD07> { [     u,          U,         udiaeresis,  Udiaeresis ] };
   key <AD09> { [     o,          O,         odiaeresis,  Odiaeresis ] };
   key <AC01> { [     a,          A,        adiaeresis,   Adiaeresis ] };
   key <AC02> { [     s,          S,           ssharp,    U1E9E      ] };
   include "level3(ralt_switch)"
};

This configuration does use the Alt Gr key (right alt) on the keyboard as switch key for umlauts. If the key is pressed, then u will transform to ü, o to ö, a to ä, and s to ß.

Make sure the new XKB file is used by running:

sudo dpkg-reconfigure xkb-data

There are multiple ways to activate the layout. The only one that does work reliably for me is to run the command:

setxkbmap -layout usde

Others, such as editing /etc/default/keyboard or using dconf-editor to change the configurations, did not work. To make sure that the command is executed, create a script in /usr/local/bin/setxkblayout as follows:

#!/bin/bash

setxkbmap -layout usde

Let this script execute on login by adding it to your autostart in the file ~/.config/autostart/xkbmap.desktop

[Desktop Entry]
Name=XKB Map
GenericName=Keyboard Layout
Exec=/usr/local/bin/setxkblayout
Terminal=false
Type=Application
StartupNotify=false
X-GNOME-Autostart-enabled=true
Comment[en_US]=Sets USDE keyboard layout
Comment=Sets USDE keyboard layout

This should enable the new layout.