Emacs: Cycle Fonts by Command πŸš€

By Xah Lee. Date: . Last updated: .

Here's commands that switch among font for current window. Especially useful for comparison of fonts.

put this in your Emacs Init File:

(defvar xah-font-list
   ((eq system-type 'windows-nt)
      "Lucida Console-10"
      "Segoe UI Symbol-12"
      "Lucida Sans Unicode-10"
   ((eq system-type 'gnu/linux)
      "DejaVu Sans Mono-10"
      "DejaVu Sans-10"
   ((eq system-type 'darwin) ; Mac
      ;; "Courier-16"
  "A list of fonts for `xah-cycle-font' to cycle from.")

(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://xahlee.info/emacs/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 (xfontToUse xstateBefore xstateAfter )
    (setq xstateBefore (if (get 'xah-cycle-font 'state) (get 'xah-cycle-font 'state) 0))
    (setq xstateAfter (% (+ xstateBefore (length xah-font-list) N) (length xah-font-list)))
    (setq xfontToUse (nth xstateAfter xah-font-list))
    (set-frame-font xfontToUse t)
    ;; (set-frame-parameter nil 'font xfontToUse)
    (message "Current font is: %s" xfontToUse )
    (put 'xah-cycle-font 'state xstateAfter)))

(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 , 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 Init: Setup Font.

You can set Ctrl+F7 and Ctrl+F8 to switch to the prev/next font. [see Emacs Keys: Define Key]

Emacs Font Setup