Emacs: Set Font by Major Mode, Switch Fonts by Command

By Xah Lee. Date: . Last updated: .

Switch Between Fixed-width and Variable-width Fonts

emacs variable-pitch-mode 2015-09-21
M-x variable-pitch-mode

Call variable-pitch-mode to toggle between fixed-width and variable-width font. (monospace vs proportional)

The font change sticks to current file/buffer only.

Proportional font is useful for reading info doc, email, etc. Proportional font is easier to read, and shows 20 or more characters per line. It also works great for coding too (except Python). Try it. You may be surprised.

(Thanks to Oscar Carlsson for suggesting variable-pitch-mode.)

Setup Proportional Font by Major Mode

Here's how to make some mode always use proportional font.

  ;; use variable-width font for some modes
  (defun xah-use-variable-width-font ()
    "Set current buffer to use variable-width font."
    (variable-pitch-mode 1)
    ;; (text-scale-increase 0.5 )
  (add-hook 'nxml-mode-hook 'xah-use-variable-width-font)
  (add-hook 'emacs-lisp-mode-hook 'xah-use-variable-width-font)
  (add-hook 'js-mode-hook 'xah-use-variable-width-font)
  (add-hook 'css-mode-hook 'xah-use-variable-width-font)
  (add-hook 'html-mode-hook 'xah-use-variable-width-font)

Alternatively, you can use the following code:

(defun xah-set-font-to-monospace ()
  "Change font in current window to a monospaced font."
  (set-frame-font "DejaVu Sans Mono" t)

(defun xah-set-font-to-variable-width ()
  "Change font in current window to a variable-width font."
  (set-frame-font "DejaVu Sans" t)

〔►see Emacs: Font Configuration

Cycle between 2 Fonts

This command cycles between 2 fonts.

(defun xah-cycle-font-2 (*n)
  "Change font in current window between 2 fonts.
URL `http://ergoemacs.org/emacs/emacs_switching_fonts.html'
Version 2015-09-21"
  (interactive "p")
  ;; this function sets a property “state”. It is a integer. Possible values are 0 to length of ξfontList
  (let (
        (-fontList '("DejaVu Sans Mono-10" "DejaVu Sans-10"))

    (setq -stateBefore (if (get 'xah-cycle-font-2 'state) (get 'xah-cycle-font-2 'state) 0))
    (setq -stateAfter (% (+ -stateBefore (length -fontList) *n) (length -fontList)))
    (put 'xah-cycle-font-2 'state -stateAfter)

    (setq -fontToUse (nth -stateAfter -fontList))
    (set-frame-parameter nil 'font -fontToUse)
    (message "Font set to: %s" -fontToUse)))

Cycle Multiple Fonts

Here's a elisp code that lets you quickly switch among your choice fonts for current window.

(defcustom xah-font-list nil "A list of fonts for `xah-cycle-font' to cycle from." :group 'font)
(set-default 'xah-font-list
              ((string-equal system-type "windows-nt") ; Windows
                 "Courier New-10"
                 "DejaVu Sans Mono-9"
                 "Lucida Console-10"
                 "Segoe UI Symbol-12"
                 "DejaVu Sans-10"
                 "Lucida Sans Unicode-10"
              ((string-equal system-type "gnu/linux")
                 "DejaVu Sans Mono-9"
                 "DejaVu Sans-9"
              ((string-equal system-type "darwin") ; Mac
                 "DejaVu Sans Mono-9"
                 "DejaVu Sans-9"
(defun xah-cycle-font (*n)
  "Change font in current frame.
Each time this is called, font cycles thru a predefined list of fonts in the variable `xah-font-list' .
If *n is 1, cycle forward.
If *n is -1, cycle backward.
See also `xah-cycle-font-next', `xah-cycle-font-previous'.
URL `http://ergoemacs.org/emacs/emacs_switching_fonts.html'
Version 2015-09-21"
  (interactive "p")
  ;; this function sets a property “state”. It is a integer. Possible values are any index to the fontList.
  (let (-fontToUse -stateBefore -stateAfter )
    (setq -stateBefore (if (get 'xah-cycle-font 'state) (get 'xah-cycle-font 'state) 0))
    (setq -stateAfter (% (+ -stateBefore (length xah-font-list) *n) (length xah-font-list)))
    (setq -fontToUse (nth -stateAfter xah-font-list))
    (set-frame-font -fontToUse t)
    ;; (set-frame-parameter nil 'font ξfontToUse)
    (message "Current font is: %s" -fontToUse )
    (put 'xah-cycle-font 'state -stateAfter)))

(defun xah-cycle-font-next ()
  "Switch to the next font, in current window.
See `xah-cycle-font'."
  (xah-cycle-font 1))

(defun xah-cycle-font-previous ()
  "Switch to the previous font, in current window.
See `xah-cycle-font'."
  (xah-cycle-font -1))

Modify the variable list “xah-font-list” above, so that you can use this function to cycle among the fonts of your choice.

To get a list of fonts in emacs, see Emacs: Font Configuration.

You can set 【Ctrl+F7】 and 【Ctrl+F8】 to switch to the prev/next font. 〔►see Emacs: How to Define Keys

Change Line Spacing

Note: different font have different line spacing.

Emacs: Set Line Spacing


Emacs: toggle-word-wrap

Font/Unicode Topic

  1. Emacs: Unicode Tutorial
  2. Emacs File Encoding FAQ
  3. Unicode Basics: What's Character Set, Character Encoding, UTF-8?
  4. Emacs: Xah Math Input Mode
  5. Emacs: Xah Unicode Browser Mode

  1. Best Unicode Fonts for Programer
  2. Emacs: Font Configuration
  3. Emacs: Set Font by Major Mode, Switch Fonts by Command
  4. Emacs: Set Line Spacing
  5. Emacs: toggle-word-wrap
  6. Emacs Lisp: Unicode Representation in String
Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs