Emacs: Mouse Config

By Xah Lee. Date: . Last updated: .

This page shows you how to config mouse buttons and mouse wheel.

Make mouse wheel not accelerate

This is useful if you have Mouse with Spin Wheel

;; 2022-03-21 this is great for logitech spin wheel
(setq mouse-wheel-progressive-speed nil)

Scroll N Lines

;; 2022-03-21 scroll just 2 lines, great for logitech spin wheel.
(setq mouse-wheel-scroll-amount '(2))

Disable Mouse Highlight

When you call list-matching-lines, in the result pane, if you move mouse to a occurrence line, there is a annoying highlight that flickers a lot, especially if the line is long.

Disable it.

(setq mouse-highlight nil)
emacs mouse-highlight 2022-03-21
emacs mouse-highlight 2022-03-21

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

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://xahlee.info/emacs/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)

Commands for Mouse Wheel

You can bind wheel to any of the following.

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

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

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 🚀

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

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


Emacs Keys

Overview

How-To

Key Tips

Emacs Pinky

Misc