Rotating between different fonts in Emacs

emacs fonts

There are two types of people: those who set on one font/color theme for the rest of their life and those like me, who constantly tweak their setup. Not that it helps productivity, but hey – RMS created .emacs for a reason!

Since typing in font names over and over again is a pain, I’ve created a neat function that can be called interactively to rotate between various programming-friendly fonts installed in the system. The list of fonts is partly mine, partly borrowed from Programming Fonts (which is awesome on its own). This allows a very quick way to see how one’s code looks with different fonts (and make a final decision for the next 3 weeks or so).

(require 'cl)                           ; required for lexical-let

(lexical-let ((current-index 0))
(defun switch-font ()
  "Rotate between programming-friendly fonts.

This function attempts to identify most of programming-friendly (mostly
monospace) fonts in the system. Each subsequent run of it switches the current
frame to the next available font allowing quick assessment of different fonts.
"

  (interactive "")
  (letrec ((font-list '("3270"
                        "Anka/Coder"
                        "Anonymous Pro"
                        "Aurulent Sans Mono"
                        "Average Mono"
                        "BPmono"
                        "Bitstream Vera Sans Mono"
                        "CamingoCode"
                        "Code New Roman"
                        "Consolamono"
                        "Consolas"
                        "Cousine"
                        "Courier New"
                        "Cutive Mono"
                        "DejaVu Mono"
                        "DejaVu Sans Mono"
                        "Droid Sans Mono"
                        "Effects Eighty"
                        "Fantasque Sans Mono"
                        "Fifteen"
                        "Fira Code"
                        "Fira Mono"
                        "Fixedsys with Ligatures"
                        "Fixedsys"
                        "GNU Freefont"
                        "GNU Unifont"
                        "Generic Mono"
                        "Hack"
                        "Hasklig"
                        "Hermit"
                        "Inconsolata"
                        "Inconsolata-g"
                        "Input"
                        "Iosevka"
                        "Latin Modern Mono"
                        "Lekton"
                        "Liberation Mono"
                        "Luculent"
                        "Luxi Mono"
                        "M+"
                        "Meslo"
                        "Monaco"
                        "Monofur"
                        "Monoid"
                        "Mononoki"
                        "Monospace"
                        "NotCourierSans"
                        "Nova Mono"
                        "Office Code Pro"
                        "Oxygen Mono"
                        "PT Mono"
                        "Profont"
                        "Proggy Clean"
                        "Quinze"
                        "Roboto Mono"
                        "SK Modernist Mono"
                        "Share Tech Mono"
                        "Source Code Pro"
                        "Sudo"
                        "TeX Gyre Cursor"
                        "Ubuntu Mono"
                        "VT323"
                        "Verily Serif Mono"
                        "saxMono"
                        "-dec-terminal-medium-r-normal-*-*-140-*-*-c-*-iso8859-1"
                        "-xos4-terminus-medium-r-normal--14-140-*-*-*-*-*-*"
                        ))
           (valid-fonts (cl-intersection font-list (font-family-list)
                                         :test 'string=)))
    (progn
      (setq current-index (mod (1+ current-index) (length valid-fonts)))
      (let ((font (nth current-index valid-fonts)))
        (set-frame-font font)
        (message (concat "Font switched to " font)))))))
Powered by Jekyll and Moonwalk