Monday, July 26, 2010

Emacs: Different fonts in different modes

I love monospaced fonts (such as Terminus or Consolas / Inconsolata), but they can be harder to read when it comes to documentation or simple conversation.

So, let's make Emacs use different fonts (monospaced and variable) depending on the mode we're in (eg: Info and ERC should not be monospaced), and let's add some shortcuts to change font type /size easily. You can do something like this with a bit of elisp in your init.el / .emacs.

;; Insidious Black Magic Bits:

;; Use variable width font faces in current buffer
(defun my-buffer-face-mode-variable ()
"Set font to a variable width (proportional) fonts in current buffer"
(setq buffer-face-mode-face '(:family "DejaVu Sans" :height 100 :width semi-condensed))

;; Use monospaced font faces in current buffer
(defun my-buffer-face-mode-fixed ()
"Sets a fixed width (monospace) font in current buffer"
(setq buffer-face-mode-face '(:family "Consolas" :height 100))

;; Set default font faces for Info and ERC modes
(add-hook 'erc-mode-hook 'my-buffer-face-mode-variable)
(add-hook 'Info-mode-hook 'my-buffer-face-mode-variable)

;; Control + scroll to change font type
(global-set-key [C-mouse-4] 'my-buffer-face-mode-fixed)
(global-set-key [C-mouse-5] 'my-buffer-face-mode-variable)

;; Shift + scroll to change font size
(global-set-key [S-mouse-4] 'text-scale-increase)
(global-set-key [S-mouse-5] 'text-scale-decrease)

And the results:

Note that you can also Shift + Left Click on a window to set fonts / font sizes.