blueFast
blueFast

Reputation: 44441

Bind key to increase / decrease font size in emacs

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?

Upvotes: 43

Views: 13077

Answers (5)

Drew
Drew

Reputation: 30708

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.

Upvotes: 16

Saddle Point
Saddle Point

Reputation: 3225

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

Upvotes: 6

Bozhidar Batsov
Bozhidar Batsov

Reputation: 56645

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.

Upvotes: 18

Reece
Reece

Reputation: 8098

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

Upvotes: 6

Stefan
Stefan

Reputation: 28571

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

Upvotes: 116

Related Questions