Bind key to increase / decrease font size in emacs

12,315

Solution 1

I think you want C-x C-+ or C-x C--.

Solution 2

I'd suggest:

(global-set-key (kbd "C-+") 'text-scale-increase)
(global-set-key (kbd "C--") 'text-scale-decrease)

While the default keybindings mentioned by @Stefan do the job, I like to have the more commonly established keybindings as well. Btw, C-x C-= increases the font size as well and C-x C-0 restores the default font size.

Solution 3

C-x C-+ and C-x C-- gives you only part of the answer: text scaling a buffer.

You can change the font size for a given frame (across all windows/buffers in that frame), or you can change the (apparent) font size for a given buffer (across all windows/frames). The latter is called text scaling, and it is what vanilla Emacs C-x C-+ and C-x C-- give you.

Library zoom-frm.el gives you both kinds of zooming with the same command. Bind the same command, zoom-in/out, to both C-x C-- and C-x C-+. It zooms either the frame or the buffer, in and out. A plain prefix arg toggles between zooming frames and zooming buffers. Bind it also to mouse keys (I use S-mouse-1 (in) and C-S-mouse-1 (out) and to the mouse wheel (in/out).

Library face-remap+.el fixes text scaling so that the window size shrinks or grows to accommodate the changing text size, which can free up screen real estate.

This EmacsWiki page has more info about this frequently asked question.

Solution 4

Check purcell's .emacs.d and his font utils.

Solution 5

And for mouse wheel changes with control key pressed:

(global-set-key [C-mouse-4] '(lambda () (interactive) (text-scale-increase 1)))
(global-set-key [C-mouse-5] '(lambda () (interactive) (text-scale-decrease 1)))

That works okay, but it's buffer local. The following code changes the frame font height for all buffers with control + mouse wheel/trackpad:

(defun change-font-height (delta)
  (set-face-attribute 'default 
                      (selected-frame)
                      :height (+ (face-attribute 'default :height) delta)))
(global-set-key [C-mouse-4] '(lambda () (interactive) (change-font-height +4)))
(global-set-key [C-mouse-5] '(lambda () (interactive) (change-font-height -4)))
Share:
12,315

Related videos on Youtube

blueFast
Author by

blueFast

Updated on June 06, 2022

Comments

  • blueFast
    blueFast about 2 years

    In my terminal (I have terminator) I can use the key combinations Ctrl + and Ctrl - to increase / decrease the font size.

    In emacs I can do the following to set the font-height:

    (set-face-attribute 'default nil :height 70)
    

    But I do not know how to increase / decrease it. How could I easily add this to my emacs configuration?

  • Drew
    Drew over 10 years
    FWIW: That removes the possibility of using C-- as a negative prefix arg, something that most people use very often.
  • Bozhidar Batsov
    Bozhidar Batsov over 10 years
    There's always M-- (which is arguably easier to press as it involves the use of just one pinky).
  • tuxdna
    tuxdna over 9 years
    Wow, I didn't know that, even after using Emacs for over 7 years now.
  • mgalgs
    mgalgs over 8 years
    Steve Purcell has factored his text scale utilities into a nice little minor mode: purcell/default-text-scale. It's available in Melpa.