Emacs: Mouse Wheel Commands

By Xah Lee. Date: .

Commands for Mouse Wheel

You can bind wheel to any of the following.

move cursor n words

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

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

move cursor n lines

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

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

(progn
  (global-set-key (kbd "<mouse-up>") 'xx-cursor-up-n-lines)
  (global-set-key (kbd "<mouse-down>") 'xx-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 xx-scroll-up-10-lines ()
  "Scroll up 10 lines"
  (interactive)
  (xx-scroll-up 10))

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

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

Emacs Init, Mouse