How to Quickly Switch Fonts in Emacs

Perm url with updates:

How to Quickly Switch Fonts in Emacs

Xah Lee, 2010-04-10, ..., 2011-03-08

Quickly Switching Fonts in Emacs

When coding in Python or viewing directories in dired, mono-spaced font is necessary. However, proportional font works great in coding too. Try it. You may be surprised.

Proportional font is easier to read, and shows 20 or more characters per line. I got used to using proportional fonts for html, xml, perl, lisp. Even if you don't like proportional font for coding, it is quite useful for reading documentation, email, irc.

One problem is that it is cumbersome to switch font in emacs. Here's a elisp code that cycle fonts.

(defun cycle-font (num)
  "Change font in current frame.
Each time this is called, font cycles thru a predefined set of fonts.
If NUM is 1, cycle forward.
If NUM is -1, cycle backward.
Warning: tested on Windows Vista only."
  (interactive "p")

  ;; this function sets a property “state”. It is a integer. Possible values are any index to the fontList.
  (let (fontList fontToUse currentState nextState )
    (setq fontList (list
                    "Courier New-10" "DejaVu Sans Mono-9" "Lucida Console-10"
                    "DejaVu Sans-10" "Lucida Sans Unicode-10" "Arial Unicode MS-10" 
    ;; fixed-width "Courier New" "Unifont"  "FixedsysTTF" "Miriam Fixed" "Lucida Console" "Lucida Sans Typewriter"
    ;; variable-width "Code2000"
    (setq currentState (if (get 'cycle-font 'state) (get 'cycle-font 'state) 0))
    (setq nextState (% (+ currentState (length fontList) num) (length fontList)))

    (setq fontToUse (nth nextState fontList))
    (set-frame-parameter nil 'font fontToUse)
    (redraw-frame (selected-frame))
    (message "Current font is: %s" fontToUse )

    (put 'cycle-font 'state nextState)

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

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

Modify the section on fontList:

 (setq fontList (list
                    "Courier New-10" "DejaVu Sans Mono-9" "Lucida Console-10"
                    "DejaVu Sans-10" "Lucida Sans Unicode-10" "Arial Unicode MS-10" 

so that you can use this function to cycle among the fonts of your choice.

You can set F6 to swich to the next font or previous font. See: Defining Your Own Keyboard Shortcuts.

Also, if you are not using emacs 23, you should upgrade, because emacs 23 switched its internal char encoding to Unicode (utf-8), and has a new font engine that supports operating system's fonts and anti-aliasing. (see: New Features in Emacs 23)

Best Fonts for Coding & Unicode

The best font i found for coding that has heavy unicode math symbols is DejaVu Sans Mono for fixed-width and Lucida Sans Unicode for variable-width. Variable-width is my normal font. I switch to mono for python, dired, calender, etc.

emacs, font DejaVu Sans Mono

Emacs using font DejaVu Sans Mono

emacs, font Lucida Sans Unicode

Emacs using font Lucida Sans Unicode.


Oscar Carlsson wrote to say there's a “variable-pitch-mode”. That's fantastic. When you turn that on, the buffer uses a variable width font, and sticks to that buffer only. To return to fixed-width font, just call it again. Give it a hotkey for easy toggling.

Changing Line Height

When reading a document, you may want to have extra space between lines, for easy reading. Here's a command that toggles line height.

(defun toggle-line-spacing ()
"Toggle line spacing between no extra space to extra half line height."
(if (eq line-spacing nil)
    (setq-default line-spacing 0.5) ; add 0.5 height between lines
  (setq-default line-spacing nil)   ; no extra heigh between lines


If you are using emacs 23, the command “toggle-word-wrap” is useful. When on, long lines are wrapped at white space. Otherwise, words are simply cut in the middle at the edge of window.

emacs23 word wrap

word-wrap off (top) and on (bottom) in emacs 23. Call “toggle-word-wrap” to toggle, or “visual-line-mode”.

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs