15

notepad++ allow me to increase the font size when I hold the Ctrl Key and rotate the mouse middle scroll button to forward.

In the same way, the when I hold Ctrl and rotate the mouse middle scroll button backward, the fond size reduces.

How can I get the same with Emacs?

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252

5 Answers5

22

with emacs23 you can add following lines to your .emacs.el:

(global-set-key (kbd "<C-mouse-4>") 'text-scale-decrease)
(global-set-key (kbd "<C-mouse-5>") 'text-scale-increase)
Alexey Voinov
  • 1,106
  • 1
  • 7
  • 8
  • I use emacs 23.1.5 on windows. but I could not make it working –  Jan 19 '10 at 13:41
  • 4
    +1 Not sure what the mouse-4 and mouse-5 are, but this works perfectly for me, so thanks! (global-set-key (kbd "") 'text-scale-decrease) (global-set-key (kbd "") 'text-scale-increase) – harpo Oct 19 '10 at 03:48
  • on a clean ubuntu installation, C-mouse-4 and C-mouse-5 are associated to quick scrolling, so I'm using M-mouse-4 and M-mouse-5 (but the s- prefix is also available). – mariotomo May 13 '11 at 12:50
  • @mariotomo If you customize the mouse-wheel-scroll-amount variable, you can change the key which is bound to quick-scrolling, and then use these keys for zooming instead. – Mark Dec 10 '13 at 21:33
  • Alas, this doesn't work for *any* text. I.e. I have in the .emacs config a line `...'(font-lock-comment-face ((t (:foreground "dim gray" :slant italic :weight bold :height 71 :family "purisa")))`. This is sets a font/size for a comments, and this font doesn't react for some reason on the `text-scale-decrease` function. – Hi-Angel Aug 05 '14 at 14:32
8

code for AlexCombas' answer:

(defun font-big ()
 (interactive)
 (set-face-attribute 'default nil :height 
  (+ (face-attribute 'default :height) 10)))

(defun font-small ()
 (interactive)
 (set-face-attribute 'default nil :height 
  (- (face-attribute 'default :height) 10)))

(global-set-key (kbd "<C-wheel-down>") 'font-small)
(global-set-key (kbd "<C-wheel-up>") 'font-big)

Edit: for a min and a max use

(defun font-big ()
 (interactive)
 (set-face-attribute 'default nil :height 
  (min 720
   (+ (face-attribute 'default :height) 10))))

(defun font-small ()
 (interactive)
 (set-face-attribute 'default nil :height 
  (max 80
   (- (face-attribute 'default :height) 10))))
Community
  • 1
  • 1
cobbal
  • 69,903
  • 20
  • 143
  • 156
  • this works. anyway where I can limit the font size to 72 maximum and 8 minimum? –  Jan 19 '10 at 13:42
  • 1
    `` and `` didn't work for me. I used `` and ``, which works fine. (I'm on ArchLinux, if it matters) – modulitos Jul 26 '15 at 10:04
  • I used the example code, and added the following to just before the final close-parentheses of functions `font-big` and `font-small`, so that the message line tells what the new height is when the commands are executed: `(message "Default face height set to %d" (face-attribute 'default :height))` – Teemu Leisti May 23 '18 at 04:54
2

Try this:

(global-set-key (kbd "<C-mouse-4>") (lambda () (interactive) (text-scale-decrease 1)))
(global-set-key (kbd "<C-mouse-5>") (lambda () (interactive) (text-scale-increase 1)))
Nate
  • 6,779
  • 8
  • 28
  • 21
1

Theoretically I can give you the answer to this, but someone more skilled than me is going to have to write the lisp I'm just a little to busy atm to figure it out for myself.

If nobody responds by tomorrow I'll hit the books and figure it out.

What needs to be done: Write a function (font-big) which does this:

  1. font-default-size = font-default-size+1`

  2. Then re-evaluate all open buffers.

Then Bind the function to a key (define-key map [C-wheel-up] 'font-big)

Then do the same for (font-small).

I hope I get at least partial credits for the idea :)

metacontent
  • 1,326
  • 1
  • 13
  • 18
1

Zoom Frame is what you want. I do exactly what you describe all the time. After loading zoom-frm.el, add some bindings such as these:

    (global-set-key [S-mouse-1]   'zoom-in)
    (global-set-key [C-S-mouse-1] 'zoom-out)
    (global-set-key (vector (list 'control mouse-wheel-down-event)) 'zoom-in)
    (global-set-key (vector (list 'control mouse-wheel-up-event))   'zoom-out)

See also: http://www.emacswiki.org/emacs/SetFonts#ChangingFontSize

Drew
  • 29,895
  • 7
  • 74
  • 104
  • you rock. I would play around these settings. I made the *zoom* with others suggestion. This one comes with emacs could reduce my emacs configuration. –  Aug 24 '11 at 07:04
  • That might well work, but as you say, using those commands requires adding `zoom-frm.el` to `.emacs.d/lisp/`, which requires adding `frame-cmds.el`, which requires adding yet further files, all huge. I balk at adding all that stuff to my Emacs setup just to get simple font-zooming functionality. @cobbal's answer does the trick without requiring anything extra. – Teemu Leisti May 23 '18 at 05:05
  • @TeemuLeisti: No. Only three files are needed: (1) `zoom-frm.el`, (2) `frame-cmds.el`, and (3) `frame-fns.el`. The files are separate precisely because someone can use #3 without #1 or #2, or use #2 without #1. If this were available only as a Zoom "package" then that would be all you could do: always load all three. Oh, and *none* of them are "*huge*": about 100KB for all 3 together, without comments. Anyway, please don't feel obliged to use any of them. – Drew May 23 '18 at 14:22
  • It's just that if one only wants to get this simple zoom functionality to work with the mouse, cobbal's answer works fine. Anyway, I take back the "huge", and meant no offence. It seems that the files implement a lot of useful functionality. – Teemu Leisti May 24 '18 at 09:55