ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

Emacs: How to Quickly Switch Fonts

Xah Lee, , …,

This page shows you how to switch between fixed-width font (monospaced font) and variable-width font. Also commands to cycle among fonts.

Switch Between Fixed-width and Variable-width Fonts

Call variable-pitch-mode to toggle between fixed-width and variable-width font. You can give it a hotkey for easy toggling. 〔☛ Emacs: How to Define Keys

The font change sticks to that buffer only. (as opposed to the current window (what emacs calls “frame”).)

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.)

Lisp Code to Cycle Fonts

Here's a elisp code that lets you quickly switch among a list of 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-forward ()
  "Switch to the next font, in the current frame.
See `cycle-font'."
  (interactive)
  (cycle-font 1)
  )

(defun cycle-font-backward ()
  "Switch to the previous font, in the current frame.
See `cycle-font'."
  (interactive)
  (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.

To get a list of fonts in emacs, use (print (font-family-list)).

You can also reduce the font list to just 2 fonts. One for fixed-width and one for variable-width. So, the command becomes a toggle between fixed/variable-width font.

You can set F6 and F7 to switch to the prev/next font. 〔☛ Emacs: How to Define Keys

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. 〔☛ New Features in Emacs 23

see also: Emacs: How to List & Set Font

Best Fonts for Coding & Unicode

One of the best font is the “DejaVu” suite. “DejaVu Sans Mono” for fixed-width and “DejaVu Sans” for variable-width. This font also contains most of the math symbols in Unicode. 〔☛ Math Symbols in Unicode

emacs, font DejaVu Sans Mono
Emacs using font DejaVu Sans Mono
emacs, font Lucida Sans Unicode
Emacs using font Lucida Sans Unicode.

For best fonts and download location, see: Best Unicode Fonts for Programing.

Changing Line Spacing

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."
(interactive)
(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
  ))

toggle-word-wrap

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.
blog comments powered by Disqus