Emacs: Set Font in Init File

By Xah Lee. Date: . Last updated: .

This page shows you how to set font for emacs in init file.

Change Font/Size Globally by Menu

Chose the menu:

  1. menu [Options ▸ Set Default Font…]
  2. menu [Options ▸ Save Options].

This changes the default font for all your current session's emacs windows/buffers, and also save it to the Emacs Init File in the custom-set-variables section. [see Emacs: M-x customize Tutorial]

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

Set Default Font and Size in Init File

put this in your Emacs Init File:

(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: Emacs Lisp: Determine OS, Emacs Version, Machine Host Name

List available fonts

(print (font-family-list))

[see Evaluate Emacs Lisp Code]

Check if a font exist

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

[see Emacs: Evaluate Elisp Code]

Set Font for Unicode Symbols

Symbols here mean misc unicode characters that are Math Symbols ∑ ∫ π² ∞, Arrows →, Keyboard Symbols ⌘ ↩ ⌫, Cultural Symbols ☭ or dingbats. But excluding emoji.

;; 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

Set Font for Chinese

How to install font

Restart emacs. Emacs should see it.

Reference

Font


Emacs Init

Init Basics

Keys

Font

Text Editing

Completion

File

Restore Things

Backup/AutoSave

Line

Appearance

Misc

Advanced Init Tips

packages