setting fonts

NOTE: if you like Org-Mode, please go and vote for it in SourceForge Community Choice Award – it's in the Most Likely to Change Change the Way You Do Everything-category.

For pleasant working with emacs, one of the more important things is choosing the right font ('face'). Especially within a windowing system, and especially with Emacs 23, there are a lot of possibilities. I am thinking from the Linux/X-Window perspective here – the support for anti-aliased fonts makes things look so much nicer – as discussed before.

On X, there are different ways to set your font. One way is through the menu (Options/Set default font.../). We can also set it in our .emacs, with (set-default-font "<font>"), or using ~/.Xdefaults. The latter method makes emacs-startup quite a bit faster, but this may have become less important in the age of emacs –daemon (Emacs 23).

barb wire

In either case – .emacs or .Xdefaults – you must provide some string describing the font. Up to Emacs-23, under X you had to use the 'barb wire'-style X font description, which you could get from a tool like UI-designers' dream xfontsel; the font description would then look something like:

  -*-bitstream vera sans mono-medium-r-*-*-*-120-*-*-*-*-iso8859-*

emacs 23

In the brave new world of Emacs 23, on X, you can also use the somewhat clearer Fontname-<size> format. You can get a list of the fonts installed on your system with the fc-list commond; if you only want to get the monospaced fonts, use

 $ fc-list :spacing=mono

For details, see the FontConfig user manual.

Note that installing fonts under X is rather easy as well these days; in most cases all you need to do is put the .ttf-files in your ~/.fonts directory and all will be find, although some program might require a restart.

Once you have chosen a font, you can put it in your ~/.Xdefaults:

Emacs.font: Envy Code R-10

and don't forget to run xrdb ~/.Xdefaults afterwards, to tell X about the changes. All of this should happen before you start emacs.

Alternatively, you can put in your .emacs something like:

(if (eq system-type 'windows-nt)
  (set-default-font "-outline-Consolas-normal-r-normal-normal-14-97-96-96-c-*-iso8859-1"))

(if (eq window-system 'x)
  (set-default-font "Inconsolata-11"))

This will set a different default font, based on whether you are running on Windows or X. You can freely adapt it to your own desires of course.

Side note: if there is yellow in the code snippet above, that is because of hightlighting lines that are too long.

face value

There are many fonts; which one is the 'best' for you, obviously depends on personal taste and also what you want to with it. As I use emacs for coding but also for reading e-mails and writing documents, there are some things that are important for me:

  • monospace
  • most important: clear and crisp, even when using smaller font sizes;
  • clearly separate O (capital O) and 0 (zero);
  • support italic display;
  • support the characters I might use (incl. accented characters and some greek ones).

Following these rules, I found the Envy Code R font to work very nicely. It's not fully Free though: Free to use but distribution prohibited. Raph Levien's Inconsolata is nice as well and truly Free; it does not provide an italic font though (at least it does not show in Emacs). Here's a list of programming fonts.


Hmmm… I wanted to write some small entry… And there is so much more to say about fonts. As often, EmacsWiki has a lot of information; for example about FontSets, which allow you to use a sort-of combination-font, which is nice if you have to work with mixed character sets (Latin, Arabic, CJK etc.).

Also, the emacs-fu entry on zooming in/out is useful in this context, even though Emacs 23 has gained something similar by default.

Also, the entry on color theming may be interesting, in this entry we only look at the default font, but you can change fonts governing only part of emacs as well; see M-x list-faces-display.

Or get information about the font at point with C-u C-x =.


Anonymous said...

The Inconsolata Medium font does display italics in Emacs- but apparently the GTK+ font selector is broken.

$ emacs -xrm 'Emacs.FontBackend: xft' -xrm 'Emacs.font: Inconsolata-12' -Q

to see if the italics work for you.
How do I specify the Emacs font from .Xresources?
Emacs.font: Inconsolata-12
doesn't work.

As for voting for org-mode- I really like it, but I think RepRap has everyone beat on scope and vision. It's hardware! Written like software! I thought it simply blew away the competition.

djcb said...

@rxfl: yeah, noticed the display thing in the font dialog, but I couldn't get italics to work in Emacs. In other apps they do (e.g. gedit), even when the font dialog has the same issue. So, maybe emacs gets confused because of some wrong info in the font files (just guessing).

Xresources should work, but make sure you run xrdb ~/.Xresources *before* starting emacs.

Anonymous said...

Inconsolata is chosen through .Xresources now- I fixed the italic problem by using the customize menu to use a different font just for the italics.

Inconsolata is pretty!

Damien Guard said...

The plan is to make Envy Code R truly free it's just right now it's really rough in places :)


paurullan said...

Gorgeous, this worked great!

Unknown said...

Hi! Sorry for commenting on a really old post, however no one else on the internet seems to explain these matters as well as you. So, question: I run fc-list :spacing=mono
Source Code Pro:style=Light

I add the line
Emacs.font: Source Code Pro
to .Xresources, run xrdb -merge .Xresources, then run emacs, and I get the following error:
Font `Source Code Pro' is not defined

The results are the same for nearly every font except for Courier, Utopia and a few others (only one-word name length that I've found)

Could you point me in the right direction?


Anonymous said...

I set the font in my .emacs file like this:
(set-default-font "-adobe-Source Code Pro-normal-normal-normal-*-12-*-*-*-m-0-iso10646-1")
You can get a list of the supported font lines like this:
M-x set-default-font
and then on Font name: you press TAB twice and the completions list is presented.