Type Diacritics in Linux

Here's how to map AltGr to type special characters in Linux.

I wanna be cute and write "niños" in my posts, but how do I get that fancy tilde to show up over the 2nd "n" (ñ, called "enye")? Or, I want to write résumé, but I just keep starting again.

These little tidbits put over the letters are diacritics. And they're not on my standard US standard layout keyboard. So, how do I type them? Here are two methods I've found so far:

Unicode Codes

First method I found is simple, but it doesn't work in the terminal. And you have to know octal unicode codes.

The combo can go a couple of ways:

  1. Press together: Ctrl-Shift-u

  2. Release

  3. Type octal code (eg, 00f1)

  4. Press Enter

Or for more of a vulcan death grip:

  1. Press together: Ctrl-Shift-u

  2. Release u only

  3. While holdin Ctrl-Shift, type octal code (eg, 00f1)

  4. Release Ctrl-Shift

This will work in GUIs like gedit, but not in vim, and I don't know unicode codes off the top of my head, so let's try the next solution.

Compose Key

Thanks to Sun Microsystems, starting in 1987, there used to be a dedicated key on your keyboard called the Compose key. Sometimes it's called the Alt Graph key and labelled AltGr. Thanks to Mac and Windows, whose OSes don't support the Compose key, keyboards these days broadly lack a Compose key.

A Compose key allows you to write characters that aren't listed on the keycaps of your computer, such as diacritics or other symbols, like ©. You would press Compose first, then a combo of usually two other keys to create the symbol.

Find the Compose Key

If there's not a dedicated Compose key, where do you find it? It might be mapped to where AltGr usually is, in the position of the right Alt key.

To see if you have a Compose key, here called "Multi_key" already mapped, run:

❯ xmodmap -pke | rg Multi_key
keycode 134 = Multi_key Multi_key Multi_key Multi_key

Turns out I have one already mapped to keycode 134, but I don't know what that key is. I can run xev to try to find this key, but I apparently don't have this key on my keyboard.

Map the Compose Key

If you don't have a key to press that's mapped to Multi_key, you'll have to map it yourself. First, identify the key that you want to map: Again, with xev, find the keycode. I'm on a Kinesis keyboard with no Right Alt, so I'm going to choose the Win/Windows key. When I press it in xev, I get:

KeyRelease event, serial 34, synthetic NO, window 0x600001,
    root 0x799, subw 0x0, time 2025078, (232,1193), root:(5228,1213),
    state 0x40, keycode 133 (keysym 0xffeb, Super_L), same_screen YES,
    XLookupString gives 0 bytes:
    XFilterEvent returns: False

The Win keycode is 133.

I'm going to use xmodmap to set it. I'm running i3, so I set that in the ~/.config/i3/config:

exec_always --no-startup-id xmodmap -e "keycode 133 = Multi_key NoSymbol Multi_key"

To use this config, restart with i3-msg restart (reload is insufficient).

To test the mapping, now when I run xev again and press the Win key, I get:

KeyRelease event, serial 34, synthetic NO, window 0x5800001,
    root 0x799, subw 0x0, time 7332864, (-2288,1461), root:(1007,1481),
    state 0x40, keycode 133 (keysym 0xff20, Multi_key), same_screen YES,
    XLookupString gives 0 bytes:
    XFilterEvent returns: False

Note the Multi_key mapping where there used to be Super_L.

Compose Characters

Now I get to type all the symbols I want. There are great mnemonics. Try these combos:

Win ~ n = ñ
Win o c = ©
Win ' e = è

To see available combos:

cat /usr/share/X11/locale/en_US.UTF-8

(Or combos online).

You'll see, for instance:

<Multi_key> <n> <asciitilde> : "ñ" tilde # LATIN SMALL LETTER N WITH TILDE

Your set. Now you can brush up your résumé.

Other Resources

  • https://help.ubuntu.com/community/ComposeKey

  • https://superuser.com/questions/59418/how-to-type-special-characters-in-linux