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.

Published by Avdi Grimm

10 Comments

  1. There are Emacs 23 cvs snapshots available for Ubuntu: https://launchpad.net/~ubuntu-elisp/+archive/ppa Emacs 23 is a dramatic improvement font-wise.

    Reply
    • Thanks, Paul, but I'm actually using Emacs 23 already (You don't need a PPA for Emacs 23; just install the emacs-snapshot packages). The above instructions are for Emacs 23. Emacs 22 is simply a lost cause when it comes to fonts.

      Reply
      • Oh! Sorry about that – I'm not using Ubuntu myself so I linked to the PPA in case the repository wasn't as up to date. But I had no idea you were using Emacs 23 anyway! The description of your troubles and your instructions had me convinced you must've been running an old 22 xft or 23 unicode build at best. There is something odd going on here – look how different my Emacs cvs build is:

        http://img114.imageshack.us/img114/6638/screens

        When I clicked “Set Default Font” in that “Options” menu, that font dialog appeared (as it does for per-buffer font setting with S-down-mouse-1). If I chose a default font from there and then clicked on “Save Options”, it would put a default font face spec in my .emacs (I don't actually do it that way myself: I prefer to use CL arguments or .Xdefaults/.Xresources).

        You can specify fonts with either GTK or fontconfig syntax, easily set fonts or increase/decrease font size per-buffer etc. and Emacs 23 font handling in general seems to me to be better than any other apps I use now so I'm disappointed that your snapshot – which does seem to be different – isn' t working so well for you.

        Reply
        • Ya know, now that you mention it, I distinctly recall getting that nice GTK font selector in the past. It's like my snapshot has regressed or something. Weird.

          Reply
        • OK, so I just now tried again and I saw that nice GTK font dialog come up. I don't know what changed. That's just bizarre. I think Emacs is trying to mess with my head.

          Reply
  2. Oh! Sorry about that – I'm not using Ubuntu myself so I linked to the PPA in case the repository wasn't as up to date. But I had no idea you were using Emacs 23 anyway! The description of your troubles and your instructions had me convinced you must've been running an old 22 xft or 23 unicode build at best. There is something odd going on here – look how different my Emacs cvs build is:

    http://img114.imageshack.us/img114/6638/screens

    When I clicked “Set Default Font” in that “Options” menu, that font dialog appeared (as it does for per-buffer font setting with S-down-mouse-1). If I chose a default font from there and then clicked on “Save Options”, it would put a default font face spec in my .emacs (I don't actually do it that way myself: I prefer to use CL arguments or .Xdefaults/.Xresources).

    You can specify fonts with either GTK or fontconfig syntax, easily set fonts or increase/decrease font size per-buffer etc. and Emacs 23 font handling in general seems to me to be better than any other apps I use now so I'm disappointed that your snapshot – which does seem to be different – isn' t working so well for you.

    Reply
  3. Ya know, now that you mention it, I distinctly recall getting that nice GTK font selector in the past. It's like my snapshot has regressed or something. Weird.

    Reply
  4. OK, so I just now tried again and I saw that nice GTK font dialog come up. I don't know what changed. That's just bizarre. I think Emacs is trying to mess with my head.

    Reply
  5. You must've offended her. 😉

    Reply

Leave a Reply

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