SetFonts

The most important info first. Here’s how to change the default font in your InitFile: use ‘M-x customize-face’ to customize the ‘default’ face.

Inspecting and Changing Fonts

Globally Change the Default Font

To change the default font for new (non special-display) frames, put either of these in your init file:

(add-to-list 'default-frame-alist '(font . FONT ))
(set-face-attribute 'default t :font FONT )

To change the default font for the current frame, as well as future frames, put either of these in your init file:

(set-face-attribute 'default nil :font FONT )
(set-frame-font FONT nil t)

where FONT is a string naming the font you want, for example, "Droid Sans Mono-10".

This should work on all platforms. As of Emacs 23, all of your system fonts including TrueType are available to Emacs.

If you are using a development snapshot for what will become Emacs 24.4 then see Emacs bug #16529.

(There needs to be an explanation for how to set fonts for particular modes or buffers here. Including a set-face-attribute hook as described above for a particular mode does not work; once you enter that mode the new font is set, but it is applied globally to all other open buffers!)

Global Fonts, .Xresources, and Emacs Daemon

I found that the only way to set fonts so that they remain consistent across emacs -nw, emacs23(-gtk), emacsclient -t, emacsclient -c, was to declare them in .Xresources and .emacs. But the declarations can’t conflict with each other. Otherwise, emacs --daemon complains. It doesn’t like (set-face-attribute ‘default nil :font FONT) and the like. For instance, to use Terminus, 9 pixel size across the board, I needed to insert

Emacs.font: Terminus-9

(set-default-font “Terminus-9”)

in my ~/.Xresources and ~/.emacs, respectively.

Change the Font for the Current Buffer

To change the font of the current buffer, you can put code like this in your init file. This example changes to an Arial font with height 140:

(face-remap-add-relative 'default :family "Arial" :height 140)

Changing the (buffer) menu fonts for GTK emacs

According to https://stackoverflow.com/questions/3454466/emacs23-buffer-menu-font-gtk say in ~/.emacs.d/gtkrc:

style "menufont"
{
  font_name = "monospace 10"  # Pango font name
}
widget "*emacs-menuitem*" style "menufont"

Using widget "*" ... will change all “toolkit” fonts (menus, dialogs).

When using gtk emacs on a remote 4k display, I would get an small black menubar with tiny white lettering.

I initially started digging into all these settings to change the menubar font size and color to be more readable.

In the end I found the GDK_SCALE, GDK_DPI_SCALE and GTK_THEME environment variables were a quick shortcut to the readability I wanted. For example I settled on:

$ GDK_SCALE=1.7 GDK_DPI_SCALE=1.7 GTK_THEME=Adwaita emacs &

You may have to fiddle because it’s a little unclear to me how the scale variables affect things, even reading this documentation: https://developer.gnome.org/gtk3/stable/gtk-x11.html

Changing Font Size

You can resize (text-scale) the text in a buffer (in all windows showing it), or you can resize the default font of a frame.

Resize (Zoom) Buffer Text Incrementally

Starting with GnuEmacs 23, you can zoom the text in the current buffer, in all frames displaying it:

BUG? – If you use a fontset with Xft, `C-x C-+’ this does not scale the secondary font. For example, If you use a fontset – dejavu-sans-mono for latin, NanumGothic_Coding for hangul(Korean) – , increasing the text size does not scale the Korean characters. – cinsk

  • ‘zoom-in/out’, ‘zoom-in’, ‘zoom-out’ (in zoom-frm.el) – The first is a more general replacement for the vanilla command (‘text-scale-adjust’) bound by default to `C-x C-+’, ‘C-x C--’, ‘C-x C-0’, and `C-x C-=’. You can bind the other two to mouse-wheel rotations, so you can use the wheel like you do in a Web browser. You can also use these same commands to zoom a frame (see below): ‘C-u’ for each command toggles between buffer and frame zooming. And ‘C-u’ while repeating toggles buffer/frame on the fly during ‘zoom-in/out’.
  • ‘doremi-buffer-font-size’ (in doremi-frm.el) – Change buffer font size incrementally using the arrow keys or the mouse wheel.
  • face-remap+.el – For all of the above, let buffer font resizing also zoom the window size accordingly (horizontally, vertically, or both). That way, you can take advantage of the space freed up by resizing (text-scaling) to a smaller font.
  • GlobalTextScaleMode – Adjust font size for all buffers at once using face remapping (Emacs 23+).

Resize (Zoom) Frame Font Incrementally

You can use any of the following to increase or decrease the font size for the current frame, effectively resizing it incrementallyzooming the text in or out.

Resize Frame Font using Lisp

You can change the size of a font for the current frame non-incrementally using ‘set-face-attribute’ with a :height parameter:

  (set-face-attribute 'default (selected-frame) :height 100)

Alternatively, you can use ‘set-frame-font’, ‘set-face-font’, or ‘set-face-attribute’ with a FONT string argument that has the size you want:

  (set-frame-font
    "-outline-Courier New-normal-normal-normal-mono-11-*-*-*-c-*-iso8859-1")
  (set-face-font
    'default
    (selected-frame)
    "-outline-Courier New-normal-normal-normal-mono-11-*-*-*-c-*-iso8859-1")
  (set-face-attribute
    'default (selected-frame) :font
    "-outline-Courier New-normal-normal-normal-mono-11-*-*-*-c-*-iso8859-1")

Installing Fonts

If you are running Linux, you may be able to run `xfontsel` from a shell. This program helps you select a font. Once you’ve found one that you like, save the specification string. In my version, clicking the “select” button near the top left copies the string to my clipboard so I can yank (paste) it into Emacs.

Either set them in your InitFile:

  (set-face-font 'menu "-jmk-neep alt-medium-r-*-*-20-*-*-*-c-*-iso8859-1")
  (set-face-font 'default "-jmk-neep alt-medium-r-*-*-20-*-*-*-c-*-iso8859-1")

Or set them in your ~/.Xresources or ~/.Xdefaults file (depending on your setup):

  *.font: -jmk-neep alt-medium-r-*-*-20-*-*-*-c-*-iso8859-1
  Emacs.pane.menubar.font: -jmk-neep alt-medium-r-*-*-20-*-*-*-c-*-iso8859-1

Actually, you may want to specify FontSets instead of a single font, if you are interested in displaying various fonts at the same time

Debian users: if your emacs startup fonts look ugly, that might be because you’ve installed the gsfonts-x11 package. gsfonts-x11 gives X the ability to display Ghostscript fonts, and they are poorly rendered on screen. To fix this, read the document that comes with gsfonts-x11, or Section 9.4.13 of the Debian Reference, "TrueType fonts in X".

As an emacs newbie, I feel this section quite incomplete. For most people, Installing a font means two things :

  • first downloading the font from the net and copy it in the proper place,
  • then select it be it from a dialog from the editor or as a line to add in a config file.

The section covers only the second point. I am quite willing to agree that emacs does thing differently for a bunch of reason, but please give me some light. (Even to tell it is some sort of X dependency, which I suspect but that doesn’t tell me what to do).

Note that arrived here after a search on “font” which, I think, seems quite a reasonable thing to do to install a new font. I didn’t see a newbie-specific page. – LionelB

For downloading fonts and installing them, there’s nothing Emacs-specific. Emacs uses your system’s fonts. If you’re using Emacs with X11, then use the X11 font installation technique (xset fp etc.). If you’re using Emacs with X11/Xft, then use the Xft font installation technique (.fonts.conf). If you’re using Emacs with MS Windows, then use the MS Windows font installation technique (Control Panel / Fonts). If you’re using Emacs with Mac, then use the Mac font installation technique (Font Book / Add Fonts).

X Logical Font Description (XLFD) python utility: https://gist.github.com/tomleo/b14a7a75e49d57c08bc1TomLeo

Great. How about porting it to EmacsLisp? ;-) – DrewAdams

Good Pixel Fonts

Testing if fonts are available?

Is it possible to test if a given font is available without an error or other side-effects? I just wrote:

    (defun font-existsp (font)
      (if (string-equal (describe-font font)
                        "No matching font being used")
          nil
          t))

but it has the side-effect of opening a description in Help if the font exists. I’m trying to set my init files to try a series of fonts and use the first available one. (I’m using Emacs 22.1.2 on OpenBSD, fwiw.) -ScottVokes

 I figured out a simpler way:
  (defun font-existsp (font)
    (if (null (x-list-fonts font))
        nil t))

I use this:

  (member font-name (font-family-list))

-MichaelChen

Also I use this:

  (find-font (font-spec :name font-name))

And so..

  (require 'cl)
  (defun font-candidate (&rest fonts)
     "Return existing font which first match."
     (find-if (lambda (f) (find-font (font-spec :name f))) fonts))
  ;; Emacs23 style font setting.
  (set-face-attribute 'default nil :font (font-candidate '"Consolas-10:weight=normal" "DejaVu Sans Mono-10:weight=normal"))

Version using dash.el (call it the same way as above):

  (require 'dash)
  (defun font-candidate (&rest fonts)
    "Return the first available font."
    (--first (find-font (font-spec :name it)) fonts))

Version using loop:

  (require 'cl)
  (defun font-candidate (&rest fonts)
    (loop for font in fonts when (find-font (font-spec :name font)) return font))

True type fonts?

I’d like to use TrueType Fonts in Emacs in various places, but Emacs handles them badly. Can someone tell me why TTF fonts are so much trouble in Emacs, and if there’s some way I could get emacs to play nicely with them anyway? --ShaeErisson

For TTF support, see XftGnuEmacs --TimotheeBesset

As of 23.3.1 TTF support is still quite defective (on Debian at least): for example, DejaVu Sans Mono 11 is rendered at ~11.5pt, and 10pt is rendered at ~9.5pt. You’ll need to set a font size of e.g. 102-108 tenths to actually get 10pt text.

See DisplayEngineForArabic, perhaps the part about the bounding boxes for the ASCII characters being used helps? – AlexSchroeder
I use TrueType fonts with Emacs on MS Windows with no problem. In fact, you even get anti-aliasing. However, I think there appears to be a problem with Emacs 21 – they work fine in Emacs 20 and 22, at least. – DrewAdams

Emacs for Gentoo

Make sure you compile with the xft flag enabled.

CarbonEmacs

Customize the default face (M-x customize-face RET default RET).

Alternatively, follow the following procedure:

  1. M-x mac-font-panel-mode and pick the font you want.
  2. M-x describe-font and copy the font name (e.g. “-apple-inconsolata-medium-r-normal--13-130-72-72-m-130-iso10646-1”)
  3. Add the following to your .emacs file:
    (set-default-font
     "-apple-inconsolata-medium-r-normal--13-130-72-72-m-130-iso10646-1")

Emacs for Mac OS X

M-x mac-font-panel-mode is not available on Emacs builds downloaded from https://emacsformacosx.com. On these builds, use the default Mac shortcut to popup the font panel, i.e, Command+T. Alternatively, use M-x ns-popup-font-panel. Then follow the instructions in the Carbon Emacs section.

From my personal experiences, there is no working-for-all-emacs solution for fontset. For current Emacs 24.x (from https://emacsformacosx.com/), below script can be one of the solution:

    (when (eq system-type 'darwin)

      ;; default Latin font (e.g. Consolas)
      (set-face-attribute 'default nil :family "Consolas")

      ;; default font size (point * 10)
      ;;
      ;; WARNING!  Depending on the default font,
      ;; if the size is not supported very well, the frame will be clipped
      ;; so that the beginning of the buffer may not be visible correctly. 
      (set-face-attribute 'default nil :height 165)

      ;; use specific font for Korean charset.
      ;; if you want to use different font size for specific charset,
      ;; add :size POINT-SIZE in the font-spec.
      (set-fontset-font t 'hangul (font-spec :name "NanumGothicCoding"))

      ;; you may want to add different for other charset in this way.
      )

– cinsk


CategoryDisplay CategoryFrames CategoryInternationalization