46

after turning on xterm-mouse-mode, any click on the in the screen returns "mouse-1 is undefined". What should I be defining "mouse-1" as? Is there some reason my mouse clicks are returning this event, and not others its suspecting?

Chris
  • 3,184
  • 4
  • 26
  • 24

4 Answers4

55

For Emacs in iTerm 2, I've found that the following bit in my ~/.emacs file works very well, providing the ability to insert the character at an arbitrary location, mark a region, and use the scroll wheel:

;; Enable mouse support
(unless window-system
  (require 'mouse)
  (xterm-mouse-mode t)
  (global-set-key [mouse-4] (lambda ()
                              (interactive)
                              (scroll-down 1)))
  (global-set-key [mouse-5] (lambda ()
                              (interactive)
                              (scroll-up 1)))
  (defun track-mouse (e))
  (setq mouse-sel-mode t)
)
Nicholas Riley
  • 43,532
  • 6
  • 101
  • 124
bfroehle
  • 1,116
  • 10
  • 12
  • Don't quote your lambdas: it's at best useless. – Stefan Sep 16 '13 at 12:45
  • 3
    Note that this solution means selected text (via left click) gets copied to the Emacs clipboard. To use the System clipboard instead, you need to use Option-Select (Mac) or Shift-Select (Linux). – ishmael Apr 13 '15 at 19:16
  • This works for only terminal but not iterm2. In term2, i have to remove "unless window-system". – Joe C Oct 13 '18 at 22:39
34

I put this in my .emacs:

(require 'mouse)
(xterm-mouse-mode t)
(defun track-mouse (e)) 
(setq mouse-sel-mode t)

and that seems to do the trick, and now a mouse click in a split changes focus to the split.

Note: I am using iterm2, and I found the info here: http://groups.google.com/group/iterm2-discuss/browse_thread/thread/8e1f2ee7db26c07d/17ac15e69c554998?show_docid=17ac15e69c554998&pli=1

DrStrngeluv
  • 364
  • 3
  • 4
  • 1
    This did not work for me on OSX + iterm2. It gave me the error of unknown mouse-4 and mouse-5 when scrolling. The answer below, by bfroehle, did work. – mr-sk Jun 16 '16 at 14:09
  • Also didn't work for me -- when I click ANSI terminal escape sequences get inserted. – TextGeek Feb 20 '19 at 15:52
7

Mac OS X's Terminal.app does not support mouse reporting. However MouseTerm is a SIMBL plugin that provides it with this feature. http://bitheap.org/mouseterm/

Install MouseTerm and put the following in your Emacs config file:

; enable mouse reporting for terminal emulators
(unless window-system
  (xterm-mouse-mode 1)
  (global-set-key [mouse-4] (lambda ()
                              (interactive)
                              (scroll-down 1)))
  (global-set-key [mouse-5] (lambda ()
                              (interactive)
                              (scroll-up 1))))
Stefan
  • 27,908
  • 4
  • 53
  • 82
hekevintran
  • 22,822
  • 32
  • 111
  • 180
1

I suspect that installing the emacs-goodies-el will provide the appropriate bindings.

George
  • 4,189
  • 2
  • 24
  • 23
  • seems like this is an ubuntu/debian package, how can I add this? I believe this is an mac configuration issue because even after issuing a (require 'mouse) a number of the function defined in mouse.el and not available in emacs. Tried the emacs version available by default and from ports/homebrew... – Chris Apr 19 '11 at 22:05
  • `(require 'mouse)` is available in the base package. – ocodo Feb 10 '13 at 22:25