45

I just switched from OSX Terminal to iTerm2, and I seem to have lost one piece of functionality. In Terminal, I could scroll through output of the less command with my mouse, because I had installed MouseTerm.

However, when I try scrolling less output in iTerm2, the window's scrollbar moves instead. Strangely, scrolling works as expected with vim.

How can I scroll less output with my mouse in iTerm2?

Ruben Verborgh
  • 3,545
  • 2
  • 31
  • 43
  • 1
    It sounds like you have some sort of third-party software installed, because Terminal doesn’t have support for sending input to the tty when you use the mouse to scroll—it only knows how to scroll the scrollbar. – Chris Page Jan 24 '13 at 18:14
  • @ChrisPage Indeed, good spot. I forgot I had installed [MouseTerm](https://bitheap.org/mouseterm/) long ago. I clarified this in the question. – Ruben Verborgh Jan 25 '13 at 13:05

6 Answers6

81

Now it is implemented in iTerm2 and can be activated by

Settings -> Advanced -> Scroll wheel sends arrow keys when in alternate screen mode and change it to Yes

image of "Scroll wheel sends arrow keys when in alternate screen mode" setting

(defaults write com.googlecode.iterm2 AlternateMouseScroll -bool true still works but it just changes the same setting)

Pylinux
  • 11,278
  • 4
  • 60
  • 67
  • 11
    One of SE's greatest weaknesses is that there's not a reliable mechanism to promote more up-to-date answers like this here, unless a lot of people vote or the OP comes back and changes the accepted answer. – krs013 Apr 02 '17 at 03:36
  • This doesn't seem to work on iterm2 build 3.1.5, any clues? – Don Giulio Nov 30 '17 at 13:41
  • @dongiulio tried restarting the terminal and or mac? – Pylinux Nov 30 '17 at 17:44
  • in iTerm2 3.1.5 this solution works perfectly, and no restarts are required – AlessioX Feb 28 '18 at 12:43
  • Worked at the beginning, but not anymore. Using v3.1.7. Any other suggestion? – Jonauz Sep 10 '18 at 11:04
  • @Jonauz Try updating and or restarting iTerm, it works perfectly fine for me on v3.2.0 – Pylinux Sep 10 '18 at 13:22
  • 5
    Doesn't work on 3.2.6 when while using git diff. Any suggestions? – Johnny_D Nov 28 '18 at 22:52
  • Git defaults to setting `less -FRSX`, where -X disables this scroll-wheel behavior (for some reason). You can enable scrolling by setting the git pager with `git config --global core.pager 'less -FRS'` – Matthias May 27 '22 at 15:12
53

The iTerm2 issue is now marked as "fixed" - but the fix is to add a hidden option, so it is not immediately obvious how to enable the new AlternateMouseScroll option.

As Filippo Valsorda (author of the patch) writes, once you install a nightly build (still not present in 1.0.0.20140629) of iTerm2, just run this command:

$ defaults write com.googlecode.iterm2 AlternateMouseScroll -bool true

restart iTerm2, and all your windows will report scroll events as up/down arrow-key escapes when the terminal is in the "alternate screen" (direct addressing) mode used by programs like less, vim, emacs, etcetera.

For applications like vim and emacs that provide additional mouse support, you will still want to enable mouse reporting mode, as this automatic mode switch only enables scrolling.

Dave Powers
  • 2,051
  • 2
  • 30
  • 34
Alex Dupuy
  • 5,984
  • 3
  • 39
  • 35
  • 1
    Awesome! Except this doesn't work when inside of `tmux`. `less` inside of `tmux` has it responding as it usually does where the scroll will access the tmux pane history. Scratching my head a little bit. I can't look into this now but I wonder if there's a way to tell `tmux` to treat `less` in a special way. I think it's that tmux believes (correctly...) that less doesn't process the mouse wheel, and so it continues to override wheel functionality. – Steven Lu Jul 17 '14 at 17:16
  • Per the comments at Filippo Valsorda's [original post](https://filippo.io/my-remote-shell-session-setup/) I suspect that you need to add ``set-window-option -g mode-mouse on`` to your ``~/.tmux.conf`` file. – Alex Dupuy Jul 22 '14 at 17:22
  • 3
    This doesn't work for me, I have iTerm 2.1.1 and I've tried all here says. – r1ckr Oct 01 '15 at 14:59
  • In Vim on iTerm2, I just scrolled up and down with the mouse wheel (didn't work), then tapped the up and down arrow keys, and a little notice popped up at the top of the window asking me if I wanted the mouse wheel to scroll in graphical programs. Clicked yes and everything is peachy. Nice. :) – Wildcard Oct 12 '20 at 19:50
3

Finally did it, I've been spending a lot of time on this looking here and there with no luck, the simplest steps are:

  1. Install the 2.9 beta version

  2. Done

r1ckr
  • 5,643
  • 5
  • 20
  • 24
1

This isn't implemented as of now.

There is an open issue in the iTerm 2 issue tracker about implementing it.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
asmeurer
  • 86,894
  • 26
  • 169
  • 240
1

Not the OP's question, but for anyone here trying to get this to work in tmux, with credit to choco via this github comment here's a snippet to add to your tmux.conf to get it to handle alternate screens (e.g. less, vim) correctly:

# Check if copy-mode or any mouse flags, then send mouse events
# or check if we're in alternate screen end send navigation keys (up, down)

bind -n WheelUpPane if-shell -Ft='#{?pane_in_mode,1,#{mouse_any_flag}}' \
        'send -Mt=' 'if-shell -Ft= "#{alternate_on}" \
        "send -t= Up" "copy-mode -et= ; send -Mt="'
bind -n WheelDownPane if-shell -Ft= '#{?pane_in_mode,1,#{mouse_any_flag}}' \
        'send -Mt=' 'if-shell -Ft= "#{alternate_on}" \
        "send -t= Down" "send -Mt="'

Note, this doesn't require the Iterm preference change mentioned in other answers, but it also isn't hurt by it.

Von
  • 4,365
  • 2
  • 29
  • 29
1

The latest version of less supports the --mouse and related flags so there's no need send arrow keys to alternate screens.

Nomas Prime
  • 1,230
  • 11
  • 16