Rotating between different fonts in Emacs

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"
                          "Anonymous Pro"
                          "Aurulent Sans Mono"
                          "Average Mono"
                          "Bitstream Vera Sans Mono"
                          "Code New Roman"
                          "Courier New"
                          "Cutive Mono"
                          "DejaVu Mono"
                          "DejaVu Sans Mono"
                          "Droid Sans Mono"
                          "Effects Eighty"
                          "Fantasque Sans Mono"
                          "Fira Code"
                          "Fira Mono"
                          "Fixedsys with Ligatures"
                          "GNU Freefont"
                          "GNU Unifont"
                          "Generic Mono"
                          "Latin Modern Mono"
                          "Liberation Mono"
                          "Luxi Mono"
                          "Nova Mono"
                          "Office Code Pro"
                          "Oxygen Mono"
                          "PT Mono"
                          "Proggy Clean"
                          "Roboto Mono"
                          "SK Modernist Mono"
                          "Share Tech Mono"
                          "Source Code Pro"
                          "TeX Gyre Cursor"
                          "Ubuntu Mono"
                          "Verily Serif Mono"
             (valid-fonts (cl-intersection font-list (font-family-list)
                                           :test 'string=)))
        (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)))))))
This entry was posted in Development and tagged , . Bookmark the permalink.