Emacs: How to Set Mouse Buttons

By Xah Lee. Date: . Last updated: .

This page shows you how to bind mouse buttons or mouse wheel to emacs commands, and other mouse configuration issues.

How to set mouse button?

Like this:

;; on Linux, make Control+wheel do increase/decrease font size
(global-set-key (kbd "<C-mouse-4>") 'text-scale-increase)
(global-set-key (kbd "<C-mouse-5>") 'text-scale-decrease)

The syntax for mouse button & wheel are different for each {Microsoft Windows, Linux, Mac}, also depends on what mouse, trackpad, etc you are using.

just call describe-key, then press the button or scroll the wheel. Emacs will display the syntax. Then, use (kbd "syntax"). Example:

(kbd "<mouse-4>") ; Linux, syntax for mouse wheel scroll up
(kbd "<mouse-5>") ; Linux, syntax for mouse wheel scroll down

;; Windows, syntax for mouse wheel scroll up/down
(kbd "<wheel-up>")
(kbd "<wheel-down>")

;; Mac OS X, syntax for mouse wheel scroll up/down
(kbd "<wheel-up>")
(kbd "<wheel-down>")

How to make mouse wheel not accelerate?

(setq mouse-wheel-progressive-speed nil)

This is useful if you have Logitech's mouse that has spin wheel.

How to define a command for clicked point?

When you bind a mouse button to a command, the command will work on the current cursor position, not clicked point.

for example:

;; on linux, make right button show char info (of current cursor position, not clicked point)
(when (string-equal system-type "gnu/linux") ; linux
  (global-set-key (kbd "<mouse-3>") 'describe-char)
  )

To make the command act on clicked point, you need to write a wrapper command that sets the position, then call the command you want. For example, suppose the command you want is describe-char on clicked point. You write a wrapper, like this:

(defun xah-click-describe-char (*click)
  "Mouse click to `describe-char' at clicked point.
URL `http://ergoemacs.org/emacs/emacs_mouse_wheel_config.html'
Version 2015-04-22"
  (interactive "e")
  (let ((p1 (posn-point (event-start *click))))
    (goto-char p1)
    (describe-char p1)))

;; on linux, make right button show char info on clicked point
(when (string-equal system-type "gnu/linux")
  (global-set-key (kbd "<mouse-3>") 'xah-describe-char-on-click)
  )

How to make mouse wheel move screen n lines?

(defun scroll-up-10-lines ()
  "Scroll up 10 lines"
  (interactive)
  (scroll-up 10))

(defun scroll-down-10-lines ()
  "Scroll down 10 lines"
  (interactive)
  (scroll-down 10))

(global-set-key (kbd "<mouse-4>") 'scroll-down-10-lines) ;
(global-set-key (kbd "<mouse-5>") 'scroll-up-10-lines) ;

You can set {⇞ Page △, ⇟ Page ▽} key to these commands too. Like this:

(global-set-key (kbd "<prior>") 'scroll-down-10-lines) ; page up
(global-set-key (kbd "<next>") 'scroll-up-10-lines) ; page down

〔➤see Emacs: How to Define Keys

How to make mouse wheel move cursor by n lines?

(defun cursor-down-some-lines ()
  "Move cursor down 10 logical lines"
  (interactive)
  (forward-line 10)
  )

(defun cursor-up-some-lines ()
  "Move cursor up 10 logical lines"
  (interactive)
  (forward-line -10))

(when (string-equal system-type "gnu/linux")
  (progn
    (global-set-key (kbd "<mouse-4>") 'cursor-up-some-lines) ; wheel up
    (global-set-key (kbd "<mouse-5>") 'cursor-down-some-lines) ; wheel down
    )
  )

How to make mouse wheel move cursor by code blocks?

((string-equal system-type "gnu/linux")
  (global-set-key (kbd "<mouse-4>") 'xah-backward-block) ; wheel up
  (global-set-key (kbd "<mouse-5>") 'xah-forward-block) ; wheel down
  )

you can find the definition of “xah-backward-block” at Emacs: Move Cursor by Paragraph or Text Block

You can also set Ctrl + wheel to do this.

How to make Control+wheel do increase/decrease font size?

;; on Linux, make Control+wheel do increase/decrease font size
(global-set-key (kbd "<C-mouse-4>") 'text-scale-increase)
(global-set-key (kbd "<C-mouse-5>") 'text-scale-decrease)

what's the default command for mouse wheel?

It's mwheel-scroll. For both scroll up/down.

Magic in mwheel-scroll

Note that the command mwheel-scroll has some special features:

if you assign the wheel to another command, such as move cursor, you do not have these features.

Advantage of Mouse Wheel Over Keys

Mouse wheel is suitable for any pair of fast-repeating commands such as {previous, next} or {backward, forward}, because you can control the firing rate and repetition rate precisely, fast, and reverse direction more easily, and is easier on the hand.

If you are looking to buy a mouse, see my Mouse Buying Guide.

Setup Mouse Click to Highlight Matching Words

Emacs Lisp: Setup Mouse Click to Highlight Matching Words

Adding Web Browser Backspace Button

Emacs: Add Web Browser Back Key/Button to Info mode

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from my keyboard store.