Emacs: Font Setup

By Xah Lee. Date: . Last updated: .

This page shows you how to set font for emacs.

Change Current Buffer Font Size Interactively by Command

Alt+x text-scale-increase
Make font size larger in current buffer.
Alt+x text-scale-decrease
Make font size smaller in current buffer.
Alt+x text-scale-adjust
Changes font size in current buffer. Call it, 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.

Change Font/Size Globally by Menu

Chose the menu [Options ▸ Set Default Font…], then menu [Options ▸ Save Options].

emacs font size menu 2021-03-18
emacs font size menu 2021-03-18
emacs font size dialog 2021-03-18
emacs font size dialog 2021-03-18

Find the Current Font Used

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

emacs describe-char 2021-09-25 TyYb
emacs Alt+x describe-char

Set Default Font/Size Globally in Init File

Use set-frame-font to set default font and size.

(when (member "Consolas" (font-family-list))
  (set-frame-font "Consolas" t t))

You can add a font size:

(when (member "Consolas" (font-family-list))
  (set-frame-font "Consolas-14" t t))

or

;; set default font
(cond
 ((string-equal system-type "windows-nt") ; Microsoft Windows
  (when (member "Consolas" (font-family-list))
    (set-frame-font "Consolas" t t)))
 ((string-equal system-type "darwin") ; macOS
  (when (member "Menlo" (font-family-list))
    (set-frame-font "Menlo" t t)))
 ((string-equal system-type "gnu/linux") ; linux
  (when (member "DejaVu Sans Mono" (font-family-list))
    (set-frame-font "DejaVu Sans Mono" t t))))

See also: Elisp: Determine OS, Emacs Version, Machine Host Name

List available fonts

(print (font-family-list))

Select the elisp code, then Alt+x eval-region. [see Evaluate Emacs Lisp Code]

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

For linux, see Standard Fonts on Linuxes

Check if a font exist

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

[see Emacs: Evaluate Elisp Code]

Set Font for Symbols

;; set font for symbols
(set-fontset-font
 t
 'symbol
 (cond
  ((string-equal system-type "windows-nt")
   (cond
    ((member "Segoe UI Symbol" (font-family-list)) "Segoe UI Symbol")))
  ((string-equal system-type "darwin")
   (cond
    ((member "Apple Symbols" (font-family-list)) "Apple Symbols")))
  ((string-equal system-type "gnu/linux")
   (cond
    ((member "Symbola" (font-family-list)) "Symbola")))))

Set Font for emoji

(progn
  ;; set font for emoji (if before emacs 28, should come after setting symbols. emacs 28 now has 'emoji . before, emoji is part of 'symbol)
  (set-fontset-font
   t
   (if (version< emacs-version "28.1")
       '(#x1f300 . #x1fad0)
     'emoji
     )
   (cond
    ((member "Apple Color Emoji" (font-family-list)) "Apple Color Emoji")
    ((member "Noto Color Emoji" (font-family-list)) "Noto Color Emoji")
    ((member "Noto Emoji" (font-family-list)) "Noto Emoji")
    ((member "Segoe UI Emoji" (font-family-list)) "Segoe UI Emoji")
    ((member "Symbola" (font-family-list)) "Symbola"))))

Set Font for Chinese

;; set font for chinese
(set-fontset-font
 t
 'han
 (cond
  ((string-equal system-type "windows-nt")
   (cond
    ((member "Microsoft YaHei" (font-family-list)) "Microsoft YaHei")
    ((member "Microsoft JhengHei" (font-family-list)) "Microsoft JhengHei")
    ((member "SimHei" (font-family-list)) "SimHei")))
  ((string-equal system-type "darwin")
   (cond
    ((member "Hei" (font-family-list)) "Hei")
    ((member "Heiti SC" (font-family-list)) "Heiti SC")
    ((member "Heiti TC" (font-family-list)) "Heiti TC")))
  ((string-equal system-type "gnu/linux")
   (cond
    ((member "WenQuanYi Micro Hei" (font-family-list)) "WenQuanYi Micro Hei")))))

Note: 2020-05-28, in emacs 26 or before, on Microsoft Windows 10, if you don't set font for Chinese, and if a buffer has Chinese character, then emacs operation such as moving cursor will be super slow, like 1/3 of a second when you press arrow key. See also: Chinese Font

Set Font for Other Languages

script-representative-chars
Variable. Alist of scripts vs the representative characters. Each element is a cons (SCRIPT . CHARS). SCRIPT is a symbol representing a script or a subgroup of a script. CHARS is a list or a vector of characters. [see Emacs: Show Variable Value]

Find a script supported in emacs by the variable script-representative-chars, then find out what's the font name in your operating system that supports that script. Then you can setup a font for the script in emacs, like this:

;; set font for cuneiform
(set-fontset-font
 t
 'cuneiform
 (cond
  ((string-equal system-type "windows-nt")
   (cond
    ((member "Segoe UI Historic" (font-family-list)) "Segoe UI Historic")))
  ((string-equal system-type "darwin")
   (cond
    ((member "Noto Sans Cuneiform Regular" (font-family-list)) "Noto Sans Cuneiform Regular")))
  ((string-equal system-type "gnu/linux")
   (cond
    ((member "Noto Sans Cuneiform Regular" (font-family-list)) "Noto Sans Cuneiform Regular")))))

(set-fontset-font
 t
 'phoenician
 (cond
  ((string-equal system-type "windows-nt")
   (cond
    ((member "Segoe UI Historic" (font-family-list)) "Segoe UI Historic")))
  ((string-equal system-type "darwin")
   (cond
    ((member "Noto Sans Phoenician Regular" (font-family-list)) "Noto Sans Phoenician Regular")))
  ((string-equal system-type "gnu/linux")
   (cond
    ((member "Noto Sans Phoenician Regular" (font-family-list)) "Noto Sans Phoenician Regular")))))

(set-fontset-font
 t
 'deseret
 (cond
  ((string-equal system-type "windows-nt")
   (cond
    ((member "Segoe UI Symbol" (font-family-list)) "Segoe UI Symbol")))
  ((string-equal system-type "darwin")
   (cond
    ((member "Apple Symbols" (font-family-list)) "Apple Symbols")))
  ((string-equal system-type "gnu/linux")
   (cond
    ((member "Noto Sans Deseret" (font-family-list)) "Noto Sans Deseret")))))

(set-fontset-font
 t
 'shavian
 (cond
  ((string-equal system-type "windows-nt")
   (cond
    ((member "Segoe UI Historic" (font-family-list)) "Segoe UI Historic")))
  ((string-equal system-type "darwin")
   (cond
    ((member "Apple Symbols" (font-family-list)) "Apple Symbols")))
  ((string-equal system-type "gnu/linux")
   (cond
    ((member "Noto Sans Shavian Regular" (font-family-list)) "Noto Sans Shavian Regular")))))

(set-fontset-font
 t
 'egyptian
 (cond
  ((string-equal system-type "windows-nt")
   (cond
    ((member "Segoe UI Historic" (font-family-list)) "Segoe UI Historic")))
  ((string-equal system-type "darwin")
   (cond
    ((member "Noto Sans Egyptian Hieroglyphs" (font-family-list)) "Noto Sans Egyptian Hieroglyphs")))
  ((string-equal system-type "gnu/linux")
   (cond
    ((member "Aegyptus" (font-family-list)) "Aegyptus")))))

How to Display Emoji on MacOS

2022-01-22 Note: this seems fixed on MacOS's emacs distro as of 2022-01-22

emacs on macOS unicode 2017 09 22 44951
Emacs on macOS does not do emoji by default, 2017-09-22

FSF GNU emacs disabled displaying color emoji on Mac, so that Mac won't work better than linux. See GNU Emacs Removes Color Emoji Support on the Mac

Solution:

download font “symbola” at Download Free Unicode Fonts

Put this in your Emacs Init File:

;; specify font for all unicode characters
(when (member "Symbola" (font-family-list))
  (set-fontset-font t 'unicode "Symbola" nil 'prepend))
emacs on macOS unicode fixed 2017 09 22 25437
Emacs on MacOS, displaying emoji using font Symbola, 2017-09-22

How to install font

Restart emacs. Emacs should see it.

Reference

Unicode and Font

Emacs Init

Init Basics

Keys

Font

Text Editing

Completion

File

Restore Things

Backup/AutoSave

Line

Appearance

Misc

Advanced Init Tips

packages