Emacs: Changing the font size on the fly

I have a tiny laptop: 8.9″ diagonally. With a 1024×768 pixels screen
resolution, things can get *pretty* small. The following functions use
the gnome-terminal-style shortcuts (Ctrl-plus, Ctrl-minus) to change
the font size without the mouse:

(defun sacha/increase-font-size ()
  (interactive)
  (set-face-attribute 'default
                      nil
                      :height
                      (ceiling (* 1.10
                                  (face-attribute 'default :height)))))
(defun sacha/decrease-font-size ()
  (interactive)
  (set-face-attribute 'default
                      nil
                      :height
                      (floor (* 0.9
                                  (face-attribute 'default :height)))))
(global-set-key (kbd "C-+") 'sacha/increase-font-size)
(global-set-key (kbd "C--") 'sacha/decrease-font-size)

On Technorati: ,

One Pingback/Trackback

  • Eric Kofoid

    Sacha —

    Thanks very much for this tip. It works perfectly and does exactly what I needed!

    Cheers,

    Eric.

  • schuerm

    Great,

    exactly what I needed!

    Thanks for sharing.

  • Dean

    Decrease works, but increase does not – whether I call from keybinding or invoke directly. Emacs 23 on Ubuntu 8.10

  • Han

    works perfectly, this is so neat.
    works on emacs 23 on Ubuntu 8.10
    Dean, I think u need to hold down the shift key for +, because + is a cap

  • Pingback: Cómo cambiar al vuelo el tamaño del texto en Emacs | lobo_tuerto()

  • Bernardo M. Rocha

    Excellent! Worked out of the box!

  • le

    thanks, works great!

  • Yangqing

    Works perfectly!!! Thanks!

  • lexsphere

    great, works perfect, exactly what I needed

  • jhapk

    Hi,
    it works for me, but it takes emacs hangs for around 5-6 seconds before it changes the font.
    Does anyone know why?
    I am running emacs 21.4.1 on fedora
    Thanks

  • jhapk

    Hi,
    I figured it out. The delay can be stopped by adding the following line to .emacs file
    (modify-frame-parameters nil ‘((wait-for-wm . nil)))
    thanks again.

  • Alex

    Did not work for me. I copied the code in my .emacs file. Did I do anything wrong? Any suggestion?

    I have to say that I rather new to linux.

    /Alex

  • Alex

    Correction:
    It works now but not with the plus and minus that are located in the right most part of the keyboard (next to numbers). Those were the one I was initially trying with.

    There is also one small issue. When I use ctrl-plus, the font size increases step by step (total 3 steps). However, when I use ctrl-minus, it shrinks the font to the smallest possible directly. A minor issue but just thougth to share it.

    /Alex