Emacs Init: 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 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.

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

"<mouse-1>"
elisp syntax for mouse left button.
"<mouse-2>"
elisp syntax for mouse middle button.
"<mouse-3>"
elisp syntax for mouse right click.
"<mouse-4>"
elisp syntax on Linux for mouse wheel scroll up.
"<mouse-5>"
elisp syntax on Linux for mouse wheel scroll down.
"<wheel-up>"
elisp Syntax on Microsoft Windows, MacOS for mouse wheel scroll up.
"<wheel-down>"
elisp Syntax on Microsoft Windows, MacOS for mouse wheel scroll down.
;; sample setting for mouse button and wheel
(cond
 ((eq system-type 'windows-nt)
  (global-set-key (kbd "<mouse-5>") 'xah-close-current-buffer)
  (global-set-key (kbd "<mouse-4>") 'describe-char))
 ((eq system-type 'gnu/linux)
  (global-set-key (kbd "<mouse-9>") 'xah-close-current-buffer))
 ((eq system-type 'darwin) ; Mac
  (global-set-key (kbd "<mouse-5>") 'xah-close-current-buffer)))

(global-set-key (kbd "M-<wheel-up>") 'xah-previous-user-buffer)
(global-set-key (kbd "M-<wheel-down>") 'xah-next-user-buffer)

Define a command for clicked point

When you bind a mouse button to a command, the command acts 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: 2016-07-18"
  (interactive "e")
  (let ((xp (posn-point (event-start Click))))
    (goto-char xp)
    (describe-char xp)))

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

Setup Mouse Click to Highlight Matching Words

Emacs: Mouse Click to Highlight Matching Words

Emacs Init, Mouse