Emacs: Mouse Config

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.

Syntax for Mouse Button or Wheel

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

Press Alt+x describe-key, then press the button or scroll the wheel. Emacs will display the syntax. Then, use (kbd syntax). Example:

(kbd "<mouse-1>")
elisp syntax for mouse left button.
(kbd "<mouse-2>")
elisp syntax for mouse middle button.
(kbd "<mouse-3>")
elisp syntax for mouse right click.
(kbd "<mouse-4>")
elisp syntax on Linux for mouse wheel scroll up.
(kbd "<mouse-5>")
elisp syntax on Linux for mouse wheel scroll down.
(kbd "<wheel-up>")
elisp Syntax on Microsoft Windows, MacOS for mouse wheel scroll up.
(kbd "<wheel-down>")
elisp Syntax on Microsoft Windows, MacOS for mouse wheel scroll down.

Make Mouse Wheel Syntax Same for Linux/Windows/Mac

(define-key key-translation-map (kbd "<wheel-4>") (kbd "<wheel-up>"))
(define-key key-translation-map (kbd "<wheel-5>") (kbd "<wheel-down>"))

Make mouse wheel not accelerate

This is useful if you have Mouse with Spin Wheel

(setq mouse-wheel-progressive-speed nil)

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:

;; make right button show char info (of current cursor position, not clicked point)
(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)))

;; make right button show char info on clicked point
(global-set-key (kbd "<mouse-3>") 'xah-describe-char-on-click)

Make mouse wheel move cursor n words

(defun xah-forward-n-words ()
  "`forward-word' 4 times."
  (interactive)
  (forward-word 4))

(defun xah-backward-n-words ()
  "`backward-word' 4 times."
  (interactive)
  (backward-word 4))

Make mouse wheel move cursor n lines

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

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

(progn
  (global-set-key (kbd "<mouse-up>") 'cursor-up-n-lines)
  (global-set-key (kbd "<mouse-down>") 'cursor-down-n-lines)
  )

Make mouse wheel move cursor by text block

(progn
  (global-set-key (kbd "<mouse-up>") 'xah-backward-block)
  (global-set-key (kbd "<mouse-down>") 'xah-forward-block))

The code for command xah-backward-block is at Emacs: Move Cursor by Text Block

Make mouse wheel move n screen

(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-up>") 'scroll-down-10-lines) ;
(global-set-key (kbd "<mouse-down>") 'scroll-up-10-lines) ;

what's the default command for mouse wheel?

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

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

If you assign a another command to the wheel, such as moving cursor command, 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, backward/forward, up/down, increase/decrease, zoomIn/zoomOut, etc, 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 Reviews.

Setup Mouse Click to Highlight Matching Words

Emacs: Setup Mouse Click to Highlight Matching Words

Add Web Browser Backspace Button

Emacs: Add Key to Info mode

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys

ELisp

ELisp Examples

ELisp Write Major Mode


Emacs Keys

Overview

How-To

Key Tips

Emacs Pinky

Keybinding Design

Misc