I am using xmodmap (and sometimes xkbcomp instead) to adjust my keyboard layout (e.g. mapping Caps Lock to the Mod3 modifier).

When I plug a USB keyboard into my laptop or insert a YubiKey (which acts like a keyboard), the settings for my laptop keyboard are immediately lost, leaving me in situations where Mod3 stops working and I cannot use my window manager any more.

How can I make my xmodmap/xkbcomp settings persistent or at least persistent per X session for my internal keyboard, so that it keeps the layout, no matter if I attach other keybaords?


You may try inputplug, a daemon which connects to your X server and listens for XInput events. It can run your script which reloads the keymap:

inputplug -c ~/.xkb_load_hook

And ~/.xkb_load_hook in my case is:


xkbcomp -I$HOME/.xkb ~/.xkb_keymap :0 2>/dev/null 1>/dev/null
xmodmap ~/.xmodmaprc

sun_id=$(xinput list | grep "HID 0430:0005" | grep -o 'id=[0-9]*' | cut -d = -f 2)

if [ -n "$sun_id" ]
    xkbcomp -i "$sun_id" -I$HOME/.xkb ~/.xkb_keymap_sun :0 2>/dev/null 1>/dev/null

The last bit detects a Sun Type 6 keyboard and applies different settings for it.


It looks like it is not X, but gnome-settings-daemon that resets my layouts when I attach a new keyboard.

This helped: udev rule to auto load keyboard layout when usb keyboard plugged in

dconf write /org/gnome/settings-daemon/plugins/keyboard/active false

to disable it from doing that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.