Ubuntu, Emacs, and Fonts

Ubuntu is a wonderful development environment in many ways, but let’s not beat around the bush: fonts in Linux have always been a disaster. It’s not as bad is it used to be; these days Ubuntu ships with some nice-looking fonts by default and apps mostly use them out of the box. Things get hinky fast, though, if you step off the beaten path. Like, say, if you want to install your own user fonts.

As it turns out, it’s trivially easy to install your own TrueType fonts in Ubuntu; it’s just not at all obvious how. Here’s the secret: simply copy the .ttf file into a directory called “.fonts” in your home directory. You can create the directory if it doesn’t already exist. Next time you start a program the font should be available.

Naturally Emacs has to throw a wrench in the works. Emacs font-handling on Linux can charitably be described as “eccentric” and more bluntly as “schizophrenic”. There are about a half-dozen different ways to specify fonts. The most obvious place is a menu item titled “Set Font/Fontset” under the “Options” menu. As far as I can tell this item is placed in the UI strictly as a diversion; it pops up a bafflingly organized menu of fonts which bears no relation to any other list of fonts on the system, and which are all hideously ugly.

If you are persistent you will eventually discover the “set-default-font” function, which is what you really want. You will type “M-x set-default-font” and then hit TAB to see a list of completions and see a list of a hojillion X11 font spec strings. After frustratedly scrolling around for a while you’ll punch up xfontsel and discover that the the font you are looking for is listed under the “unknown” foundry, or something equally unpredictable. Sensing victory close at hand, you’ll type in the whole font spec (you do know the ISO designation for your native character set, right?) and hit RET.

And then Emacs will spit out an error about it being an “undefined font”.

The completion list, as it turns out, is just another clever ruse. Emacs actually has its own syntax for specifying fonts. I don’t claim to understand this syntax. What I do know is that entries of the form [font name]-[font size in points] seem to work nicely.

So, to summarize, if you want to try out a new TrueType font (for instance, Anonymous) in Emacs, here are the steps:

  1. Put the .ttf file in ~/.fonts, creating the directory if needed.
  2. Type M-x set-default-font RET "Anonymous-10" (without the quotes)
  3. Enjoy your new font.

UPDATE: Emacs informs me that set-default-font is actually deprecated and I should be using set-frame-font instead, Also, if you want to persist this configuration the best way to do it appears to be by adding (font . "Anonymous-10") to default-frame-alist. The Emacs documentation recommends using your ~/.Xresources file for this instead, but in my experience getting X Resources to “take” is something of a crap shoot.

UPDATE 2: If you like the Inconsolata font, do not install the ttf-inconsolata package on Ubuntu 8.10 (or lower). It is broken, and it will override your .fonts version of Inconsolata with it’s nasty brokenness.

UPDATE 3: As of Ubuntu 12.10, I can now report that the newer “fonts-inconsolata” package seems to work fine.