TL;DR

For Danish Linux users who want to use the US keymap but still write Danish characters, the following commands enable the insertion of æøå with the ;'[ keys while holding the Left Ctrl key:

setxkbmap -option -option ctrl:swapcaps us

# Map (in order):
# - caps lock key to control
# - left ctrl key to Mode_switch
# - Semicolon key to support ae with Mode_switch
# - Apostrophe key to support oslash with Mode_switch
# - Bracketleft key to support aring with Mode_switch
xmodmap -e "keycode 66 = Control_L NoSymbol Control_L" \
        -e "keycode 37 = Mode_switch NoSymbol Mode_switch" \
        -e "keycode 47 = semicolon colon ae AE" \
        -e "keycode 48 = apostrophe quotedbl oslash Oslash" \
        -e "keycode 34 = bracketleft braceleft aring Aring"

In addition to enabling Danish characters as described, this also swaps the Caps lock and Right Ctrl keys.

Details

Looks like I’ve finally solved one of the last major annoyances in my Linux setup.

As a programmer I like to use the US keyboard layout because brackets and other special symbols are much more conveniently located than in the DK layout.

However, I still need to write in Danish quite often. So I had set up shortcuts to select a keymap: Super-u would select the US keymap, Super-i would select the DK one.

I’ve never really been happy with this solution though. It requires me to remember two layouts for all punctuation, and switching between layouts all the time is annoying.

Also, all I actually need from the DK keymap are the special Danish letters (æ, ø and å) so it seems silly to switch the entire keyboard layout just for that.

Today I finally took the time to read the documentation for xmodmap and understand how I could get around this issue.

The missing piece for me was the Mode_switch keysym. Using the semicolon key (keycode 47) as an example, here is the default US xmodmap config for it:

keycode 47 = semicolon colon semicolon colon

The key is mapped to four keysyms, enabled with these modifiers respectively:

  • No modifier
  • Shift
  • Mode_switch
  • Mode_switch + Shift

This is exactly what I needed: A modifier key to enable an alternative use for the key. Not in use by default - the last two keysyms are identical to the first two and in fact Mode_switch is not even mapped to a key I have on my keyboard. But I can change that.

I already had Left Ctrl swapped with Caps Lock, and I very rarely use Caps Lock as a feature anyway, so I can spare the left Ctrl for the Mode_switch instead.

Now, with a working Mode_switch I can instead configure the semicolon key with:

keycode 47 = semicolon colon ae AE

This allows me to hold down Left Ctrl, press the semicolon key and get an æ on the screen.

Similar for the apostrophe and left bracket (' and [) keys:

keycode 48 = apostrophe quotedbl oslash Oslash
keycode 34 = bracketleft braceleft aring Aring

This enables me to stay on the US layout and just hold down the left Crtl key when I need to enter a Danish character.