Emacs: Set Font

By Xah Lee. Date: . Last updated: .

This page shows you how to set font for emacs.

How to change font size?

Call text-scale-adjust, then press 【+】 to increase, 【-】 to decrease. You can press them repeatedly. Press【0】 to set it back to default size. Press any key to exit.

Or, call text-scale-increase.

(text-scale-increase 2)  ; 2 steps larger
(text-scale-normal-size) ; default size
(text-scale-decrease 2)  ; 2 steps smaller

You can make mouse wheel with Ctrl key to increase/descrease font size. See: Emacs: How to Set Mouse Buttons.

To set font size globally in init file, just put the point size in the font name:

(add-to-list 'default-frame-alist '(font . "DejaVu Sans Mono-10"))

〔➤see Emacs Lisp: Determine OS, Emacs Version, Machine Host Name

How to find the current font used?

Call describe-char, then look at the line in “display:”. It shows the font used for the character under cursor.

here's a sample output:

             position: 4968 of 17021 (29%), column: 23
            character: d (displayed as d) (codepoint 100, #o144, #x64)
    preferred charset: ascii (ASCII (ISO646 IRV))
code point in charset: 0x64
               script: latin
               syntax: w 	which means: word
             category: .:Base, L:Left-to-right (strong), a:ASCII, l:Latin, r:Roman
             to input: type "C-x 8 RET HEX-CODEPOINT" or "C-x 8 RET NAME"
          buffer code: #x64
            file code: #x64 (encoded by coding system utf-8-unix)
              display: by this font (glyph code)
    xft:-unknown-DejaVu Sans Mono-normal-normal-normal-*-12-*-*-*-m-0-iso10646-1 (#x47)

How to set default font in emacs init file?

;; set a default font
(when (member "DejaVu Sans Mono" (font-family-list))
  (set-face-attribute 'default nil :font "DejaVu Sans Mono"))


;; set default font in initial window and for any new window
 ((string-equal system-type "windows-nt") ; Microsoft Windows
  (when (member "DejaVu Sans Mono" (font-family-list))
    (add-to-list 'initial-frame-alist '(font . "DejaVu Sans Mono-10"))
    (add-to-list 'default-frame-alist '(font . "DejaVu Sans Mono-10"))))
 ((string-equal system-type "darwin") ; Mac OS X
  (when (member "DejaVu Sans Mono" (font-family-list))
    (add-to-list 'initial-frame-alist '(font . "DejaVu Sans Mono-10"))
    (add-to-list 'default-frame-alist '(font . "DejaVu Sans Mono-10"))))
 ((string-equal system-type "gnu/linux") ; linux
  (when (member "DejaVu Sans Mono" (font-family-list))
    (add-to-list 'initial-frame-alist '(font . "DejaVu Sans Mono-10"))
    (add-to-list 'default-frame-alist '(font . "DejaVu Sans Mono-10")))))

The above code sets a font as default for both initial and default window.

How to change font?

Chose the menu 〖Options ▸ Set Default Font…〗, then 〖Options ▸ Save Options〗.

Or, call set-frame-font then give a string of the font name.

Here's the elisp code:

(set-frame-font "DejaVu Sans Mono-10")  ; set font for current window
;; set font for all windows. keep window size fixed
(set-frame-font "DejaVu Sans Mono-10" t t)

;; set font for all windows. don't keep window size fixed
(set-frame-font "DejaVu Sans Mono-10" nil t)

How to list fonts?

(print (font-family-list))

Select the elisp code, then call eval-region. 〔➤see Emacs: How to Evaluate Emacs Lisp Code

You can see the result in “*Messages*” buffer. view-echo-area-messagesCtrl+h e】.

(info "(elisp) Face Attributes")

To list fonts in Linux, in terminal, type fc-list.

For a sample output of fc-list, see: linux_ubuntu_default_fonts.txt.

How to check if a font exist?

;; returns true if Symbola exists
(member "Symbola" (font-family-list))

How to install a font to emacs?

Just install to the OS normally. Restart emacs. Emacs should see it. 〔➤see Linux: How to Install Font

How to set font for a range of Unicode characters?

;; specify font for all unicode characters
(when (member "Symbola" (font-family-list))
  (set-fontset-font t 'unicode "Symbola" nil 'prepend))

Call describe-function on set-fontset-font for detail. (info "(elisp) Fontsets")

This is very useful. Because, even if you have Unicode font, but emacs still does not display some Unicode emoji. For example, on latest iBook as of out of the box, Mac OS X, emacs won't display these characters: 😃 😄 😱 😸 👸 👽 👍. More at Unicode Emoticons, Faces 😃 😄 😱 😸 👸 👽 👍.

See also: Fonts for Unicode 6 Emoticons

See also: How to Quickly Switch Fonts in Emacs.

(info "(emacs) Fonts")

Best Fonts for Coding and Unicode

One of the best font is the “DejaVu” suite. “DejaVu Sans Mono” for fixed-width and “DejaVu Sans” for variable-width. This font also contains most of the math symbols in Unicode. 〔➤see Math Symbols in Unicode

emacs, font DejaVu Sans Mono
Emacs using font DejaVu Sans Mono
emacs, font Lucida Sans Unicode
Emacs using font Lucida Sans Unicode.

Best Unicode Fonts for Programer

Best Unicode Fonts for Programer

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from my keyboard store.