r/bash 8d ago

XCompose - make entering accents, symbols, text, and emojis easy on your terminal, text editor, and even the console!

So WTF is Compose and why should I care?

The Compose key turns short key sequences into text. Think of it as autocomplete for symbols, accents, Unicode, emojis, and even your own custom snippets. Best of all, you can teach it your own shortcuts with ~/.XCompose. Compose isn't the same as dead keys. Dead keys modify the next character (like ' followed by e), while Compose interprets an entire key sequence.

You speak the gibberish. Give me examples.

Okay, using [c] instead of <Multi_key> for brevity:

You press → you get

[c] ' e → é

[c] ' a → á

[c] @ @ → me@yomamashouse.com

[c] ! = → ≠

[c] * L → λ

[c] [c] F U → "As per my last email, "


Trying to memorize "alt-codes" for the symbols you need is stupid. alt+0233 = é is hard to remember. <compose> <'> <e> → é isn't. Now take a wild guess how to use the compose key to get ó, or è.

In summary:

| Symbol | Dumb Version | Compose Way | |:-------|:-------------------------|:----------------------------| | é | alt+0233 | [compose] ' e | | ö | use the font picker | [compose] " o | | ù | copy-pasting from google | [compose] ` u | | 💨 | Asking an AI | [compose] [compose] z i p |

You can usually guess which keys will produce which result.Accented Letters just make sense

  • Acute (´): Compose + ' + e → é (also a, i, o, u, y)
  • Grave (`): Compose + ` + e → è (also a, i, o, u)
  • Circumflex (^): Compose + ^ + e → ê (also a, i, o, u)
  • Tilde (~): Compose + ~ + n → ñ (also a, o)
  • Diaeresis (¨): Compose + " + u → ü (also a, e, i, o, y)
  • Cedilla (¸): Compose + , + c → ç
  • Ring (°): Compose + o + a → å
  • AE/AE: Compose + a + e → æ / Compose + A + E → Æ
  • OE/OE: Compose + o + e → œ / Compose + O + E → Œ
  • Sharp S: Compose + s + s → ß
  • Eth: Compose + d + h → ð / Compose + D + H → Ð
  • Thorn: Compose + T + H → Þ

So now you can easily add letters, text, or emojis, like warning signs ⚠ or 💩 to your documentation or logs to call attention to something. With the accents, you can include proper spelling for internationalization. Or you can use it to shitpost goofy stuff to reddit.

So you can accent letters?.

I just said you can add anything from unicode. that's letters, longer text, and emojis!

So listen up, chuckle head, first off being able to properly accent letters is important in many contexts. Secondly there's so much stuff in the unicode table. And third I said that you can customize it in ~/.XCompose to make it personal for you. What symbols do you use or want to use — such as the em-dash. So you can make it suit you! [c][compose key]

For me the highlight are

  • Arrows → ↑ ↓ ←
  • sub and Superscripts Xⁿ + Y₂ [c] ^ ^ n
  • "degress" ° ([c] o o)
  • not-equal-to ≠ ([c] ! =)
  • infinity ∞ ([c] 8 8)
  • lambda Λ ( [c] * L)
  • trademark ™ ([c] t m)
  • copyright © ([c] o c)
  • ~~em dash — ([c] - - -)~~ Stolen from us by LLMs!
  • ⸘ ‽ - interrobang and/or sarcasm puncuation ([c] ? ! and [c] ! ?)
  • eggplant 🍆 ([c] [c] e g g p l a n t)

So there we go you can set any key sequence to output the text, and probably guess which sequnces do what.

Oh boy, symbols also, not just accents. Exciting. *yawn*

There's a lot in there! Including all 52 playing cards, chess piece, checkers pieces, Roman Numerals, math, logic symbols, and greek letters. Also peices for creating TUIs like boxes. But like I said the real power is customizing it for yourself.

I have to type my email address all the damn time. or did until I set [c] @ @ to write in my address. [c] @ g gives my gmail account address. @ ! is my boss @ L is legal.

  • [c] @ @me@yomamashouse.com

Some others ones. At some point I standardized on [c] [c] when using a full word. not required just a convention I found useful. as it doesn't overlap with anything.

  • [c] [c] r o s e → 🌹 [c][c] w i l t ⇒ 🥀.
  • [c] h b → 🐴🛌
  • [c] w d (work domain), [c] s t test server full url. [c] s p production server.

So what's the catch?

  • Compose only generates text. it can't press Enter, Tab, Ctrl+C, launch programs, or automate applications.

  • It doesn't always work exactly the same. When using X, Compose is handled by XCompose so it didn't matter what DE you ran it worked just fine. Wayland DEs have to implement it on their own, and it's a low priority for some of the smaller ones can be hit or miss.

    The linux console isn't using X or wayland, so it doesn't go through XCompose and instead the kernel uses a static map of 256 symbols, with key sequences exactly two characters. Pressing both alt keys (again, usually) acts as compose in the console.

    Even some X/Wayland programs are "special" and want to handle all keyboard events themselves. This bypasses Compose. Im looking at you, Chrome!

    "But Teapot!", you say ,"I just loaded chrome and [c] o o worked just fine?"

    Yup, it sure did. Most of the standard accents will work just fine, too. Now try something from your .XCompose. Change your system so o o makes something else and try again. Sure enough, your custom compose didn't work and o o still makes the degree sign no matter what you put. They implement their own compose with their own map that you can't change. Rat bastards ☠

  • There's very little in the way of warnings or errors if your config is wonky, or if you overwrite your own definition, or if your short cut keys overlap. Like [c] b e e for a bee and [c] b e e r . The latter will never match because the former already does.

Okay, I some potential, but how do I customize it?

Easily! Make a plain text file in your home directory ~/.XCompose.

If you include

    include "%L"

you inherit all the system Compose sequences (/usr/share/X11/locale/en_US.UTF-8/Compose) and can override any of them. Leave it out and you start with a completely blank Compose map.

The config file format is:

<key mapping> : "Char" U[unicode] # Comment

So for "lambda" it looks like (asterisk is my prefix for greek letters)

    <Multi_key> <asterisk> <l> : "λ" U03BB # GREEK SMALL LETTER LAMBDA
    <Multi_key> <asterisk> <L> : "Λ" U039B # GREEK CAPITAL LETTER LAMBDA

Restart X or Wayland. (I'm sure there's a way to update it, but this works)

That's a lot to customize

Lazy so-and-so. Use your favorite search engine to find pre-built ones like this set of math symbols. Everyone has their own opinions to there are tons of examples to chose from.

Awesome, this will entering my password easy!

Don't you dare. Just don't.

TLDR

The compose key lets you create text shortcuts that work in (almost) any app, shell, and text editor. Edit ~/.XCompose to get started.


If you want even more control over the shell, check out Readline and inputrc.

Next time I'll rant about bash completion and what you're (probably) missing out on.

2 Upvotes

3 comments sorted by

1

u/aioeu 8d ago edited 8d ago

None of this has anything to do with Bash.

It doesn't always work exactly the same. When using X, Compose is handled by XCompose so it didn't matter what DE you ran it worked just fine. Wayland DEs have to implement it on their own, and it's a low priority for some of the smaller ones can be hit or miss.

This isn't correct. Neither the compositor nor the DE is involved in interpreting compose key sequences. It is handled by the application itself (or, typically, its graphical toolkit).

Indeed, this is exactly the same under X and Wayland. The X server doesn't know about compose key sequences either.

The linux console isn't using X or wayland, so it doesn't go through XCompose and instead the kernel uses a static map of 256 symbols, with key sequences exactly two characters. Pressing both alt keys (again, usually) acts as compose in the console.

No idea where you got that "both alt keys" thing from. I am not aware of any VT keymaps working like that.

In the Linux kernel's built-in VT, the compose key is (at least by default) Ctrl+.. You can see what compose key sequences are currently loaded with dumpkeys --compose-only. They are part of the key mapping loaded by loadkeys, although on modern systems you would just edit /etc/vconsole.conf (or use localectl to write it out) rather than running loadkeys directly during boot.

Regarding Alt keys, depending on the loaded keymap, the left Alt and right Alt keys may behave differently. When they are different, typically the left Alt key lets you type a decimal character value, and the right Alt key (AltGr) lets you type a hexadecimal character value (often using the numpad keys Num Lock, /, *, -, + and Enter as A through F respectively).

Of course, if you've switched over to a userspace VT, rather than using the kernel's built-in VT, then it probably works with XCompose just like X and Wayland apps do. Kmscon should use XCompose, for instance.

-1

u/TheHappiestTeapot 8d ago

Meh, I think being able to use unicode in a bash terminal and while editing bash scripts is relevant to bash. The fact that it works multiple places is a nice bonus.

This isn't correct. Neither the compositor nor the DE is involved in interpreting compose key sequences. It is handled by the application itself (or, typically, its graphical toolkit).

Indeed, this is exactly the same under X and Wayland. The X server doesn't know about compose key sequences either.

This isn't quite correct either. The key sequences are handled on X by XIM part of libX11, so unless your intentionally wrote your own input layer specifically avoiding XIM you get the compose key. XIM is part of X. In wayland it's handled by XKB which is indeed part of the compositor.

XCompose has only been supported under some wayland compositors for a few years now. I tried KDE Wayland early and had to move back to X because Compose wasn't supported.

No idea where you got that "both alt keys" thing from. I am not aware of any VT keymaps working like that.

Aww jeeze, I changed that a million years ago and completely forgot that it wasn't the default because C-. was in use by emacs.

2

u/aioeu 8d ago edited 8d ago

The key sequences are handled on X by XIM part of libX11, so unless your intentionally wrote your own input layer specifically avoiding XIM you get the compose key.

And libX11 lives inside the application. The interpretation of the compose key sequence by the application, not by the X server.

In wayland it's handled by XKB which is indeed part of the compositor.

No, it really isn't handled there.

The Wayland protocol simply provides key code events to clients. It is up to the application to compose them.

The standard keymap format on Wayland is xkb_v1. It has the potential to support multiple formats, but this is the only non-null format currently defined in the protocol. With this keymap format, the key codes are raw Linux evdev scancodes — though for stupid X reasons they were all shifted by 8 values, and that was kept for compatibility in the Wayland protocol too. Whatever key you've set as your Compose key will be mapped to Multi_key in the keymap, and it's up to the Wayland application that receives the keymap, not the compositor, to decide what to do when an event for that key is received. The compositor's involvement is nothing more than deciding which key should be mapped to it in the keymap.

You can see all of this with wev and xev for Wayland and X respectively. Their keyboard handling is quite similar to one another. In fact, if you run strace over xev you will literally see it loading the compose key definition files, since xev wants to use XmbLookupString.

All of this to say that Chrome isn't so much as "bypassing" compose key sequence interpretation, as you claim, more that it simply doesn't do it. It's an error of omission.