April 10, 2015 - Bryce Harrington

Custom Compose Keys on Ubuntu

The Compose key is awesome, and I think Linux distributions should include this in all keyboard layouts by default. You’re probably thinking, “Wait, ‘Compose’?? There’s no key on my keyboard labeled ‘Compose’, what the heck is this guy talking about? And why would I need it, anyway?”

I’m a mono-lingual USian. Now, I had a few years of German in high school but ach, nein, it really didn’t take. However, with today’s multicultural, globalized Internet, I’ve gained friends and colleagues from all over the globe with all sorts of odd foreign letters in their names, and I want to refer to them properly.

Not too many years ago, this was a hard thing to do. The Internet communicated in so-called “plain text”, which consisted of just the basic English letters, numbers and symbols. This Internet alphabet was originally created and standardized by the Teletype industry, who named it the American Standard Code for Information Interchange (ASCII). When the World Wide Web first started, it relied on ASCII for its web pages. So, back in the day, everything online was plain ASCII text, and anything that wasn’t showed up as weird empty squares unless you did a lot of messing around with fonts, encoding rules, and the like.

But today, I can use all the strange letters and symbols I want thanks to something called ‘Unicode’. Unicode is basically a super-set of all font symbols – French, Thai, Algonquin, you name it. It’s like the uber-alphabet. You’ll still need a Unicode-supporting font, but most standard fonts include character symbols for a sizable proportion of the Unicode set. On Ubuntu, for example, they install their own ‘Ubuntu’ font which supports an impressive range of Unicode symbols.

Unicode also supports a huge variety of useful and fun symbols beyond just funny foreign characters. Bullets and asterisks, smiley faces and snowmen, Greek letters and roman numerals, arrows and musical notes, and other such wingdings galore.skull_line

So, how do you put them into your documents? The lazy way is to just cut and paste. But hey, cut and paste works and is easy. Try copying this: ☠. Now open your favorite word processor and paste it in. Arr, ye be enlarging the font to 48. Arr.

A slightly less lazy method is to launch the Character Map under Accessories (in Ubuntu) and drag and drop the symbol to your editor. Give it a shot, and look through all the wild and wonderful common characters. I took tons of math in college and surface integrals were my bane, but I managed to avoid volume integrals (∰ ) yikes!

Every Unicode character has a code. Like U+2620 for that thar skull’n’bones. If you know the code you can enter it (in Ubuntu) by holding Ctrl+Shift+u, then the code, and then Enter. But who’s going to remember all those hex strings? This leads us to understanding the unicode_symbolsCompose key’s awesomeness. Instead of memorizing hex codes, you can use more memorable characters. You tap the Compose key, followed by 2 or more keys to produce the given Unicode character. For example, ” t m” prints ™, ” ` a” prints à, and ” = L” prints ₤.

Try it Yourself

You probably don’t actually have a literal “Compose” key on your keyboard. Digital Equipment Corporation and Sun used to manufacture keyboards with actual physical keys labeled “Compose” on them. Since Microsoft Windows and OSX never included compose support by default, keyboards for PC hardware didn’t bother with them. Thus, using Linux on PC hardware means we have to map the Compose key behavior to some other infrequently used key.

I like to turn my right Alt key into Compose. I can still use the left Alt for hotkey combos and other things. Here’s how to enable it on Ubuntu 14.04:

  1. Open System Settings, and navigate to the Keyboard Configuration
  2. Select the Shortcuts tab, then Typing
  3. Click Compose Key, and change “Disabled” to “Right Alt” (or whatever you prefer from the list)
  4. Exit out of Keyboard Configuration

Let’s give it a test! Open your favorite text application and then hit and release the right Alt key, followed by the letter o and then – (minus). You should see ō. As far as I know there isn’t an official standard for these Compose sequences, but people have cleverly thought up a bunch of good ones. These come by default when enabling the Compose key on Ubuntu. The defaults add keys needed for typing the special letters of various languages, along with a scattering of symbols.

There’s a particularly good collection of Compose sequences available via the kragen/xcompose project on github. This includes tons of arrows, stars, math symbols, small caps letters, funky Latin letters, the entire Greek alphabet (upper and lower) along with Greek punctuation symbols, fractions, Roman numerals, dingbats, a very rich set of planetary and astrological symbols, and both a full deck of playing cards and a full chess set. You know you want ’em!

But wait, there’s more! You can go beyond these and provide your own custom Compose sequences in your ~/.XCompose file. The format of this file is:

<Multi_key> […keys…] : “<character[s]>” [UNICODE] # Comment

The comment is optional of course. The UNICODE number seems also not to be necessary but is handy to list for your own reference purposes I guess.

So for example, let’s add a couple bullets to this file:

<Multi_key> <asterisk> <O> : “•” U2022 # BULLET

<Multi_key> <asterisk> <o> : “◦” U25E6 # WHITE BULLET

Let’s test and see if your system supports .XCompose in applications. Launch xterm, and then hit and release the Alt key followed by an asterisk (Shift+8 on my US keyboard) and the lower case letter ‘o’. You should see a small empty circle.

If it didn’t work, the first thing to try is setting your Input Method to xim, by adding this line to the top of your ~/.gnomerc (or perhaps ~/.profile), and then log out and back in again:

export GTK_IM_MODULE=”xim”

Ubuntu 14.04 by default uses Gnome’s gtkim, which doesn’t permit user configuration of key combos. The above setting will switch it back to xim.

Once you’ve verified your ~/.XCompose is getting parsed and used, you’re all set to have some fun. Pull up the table of Unicode symbols and look through it for stuff you want to use.

The check mark can be useful for your todo lists:

<Multi_key> <at> <slash> : “✓” U2713 # CHECK MARK

Personally I think the square root symbol makes for a more distinctive check mark:

<Multi_key> <v> <slash> : “√”

You’re not limited to single characters either. For instance, you can add shortcut sequences for commonly typed strings:

<Multi_key> <m> <e> : “John Hancock”

This can be a huge time saver if you find yourself typing the same thing over and over again, like doing patch review:

<Multi_key> <r> <b> : “Reviewed-by: John Hancock “

The compose key’s functionality is rather odd, but it’s not unique; it’s one of a set of functions called “dead keys”. A “dead key” is a key that appears to do nothing when you first press it, but modifies the output of the next key that you push. Compose is one type of dead key, but there are others that add specific decorations to letters. For instance, the ‘dead-acute’ key places an acute on top of letters, ala áéíóú.

Various languages also have graves, macrons, cedilla’s, hooks, breves, and more. Keyboards for these languages may have dedicated dead keys for adding these bits to letters. Even on boring US style keyboards, you can select alternative keyboard layouts that map in dead-key functionality to lesser used keys or key combinations. Impress your professors!

As you can see, compose key sequences are a convenient way to access the wealth of Unicode symbols, but getting your system configured to use them is a bit involved. Hopefully in the future Linux distributions will improve functionality by providing the Compose key enabled by default. This would make the basic international letters far more accessible to all users.

 

Image Credits: Wikimedia Commons
Bryce Harrington

About Bryce Harrington

Bryce Harrington is a Senior Open Source Developer at the Samsung Open Source Group focusing on Open Source Graphics. Prior to Samsung, he lead Canonical, Ltd.'s Ubuntu X.org team, and focused on stabilization of the graphics and input infrastructures for the Ubuntu distribution. Bryce began his career in the aerospace industry as a spacecraft propulsions engineer at The Aerospace Corporation, Hughes Space and Communications and TRW. Later, he joined the Open Source Development Labs as a Senior Performance Engineer working on NFSv4 testing and development of automated test systems. He is a founder and developer of the Inkscape project and serves as Chairman of the Inkscape Board. Bryce has a BS-AE from USC and an MS-AE from Caltech.

Image Credits: Wikimedia Commons

Linux tips and tricks / User / Workflow /

Comments

Leave a Reply to The Basics of Input Methods in Weston - Samsung Open Source Group Blog Cancel reply

Your email address will not be published. Required fields are marked *

Comments Protected by WP-SpamShield Anti-Spam