403

Mouse scrolling doesn't work in tmux the way it works when I run shell without tmux (in Gnome Terminal). It seems tmux sends mouse scroll events as if I pressed Up/Down keys. But I want it to scroll though the shell output history. Is there a way to make tmux work like this?

Note: I know how to scroll with the keyboard (thanks to another question here).

I tried mouse scrolling in two versions of tmux:

user31494
  • 4,143

14 Answers14

413

To scroll within history of the output You would use ^b + [ You can then use M+V to page up and ^V to page down. I don't know if You can use the real PgUp and PgDown though. My terminal does not send these keys to the tmux. Instead it scrolls itself and not the tmux history.

To exit the copy mode, press ESC

To use your mouse in this mode (called copy mode) press ^b + : and enter following:

setw -g mouse on

Note: In tmux < 2.1, the option was named mode-mouse, and not mouse

Now when You change to the copy mode you can use your mouse to scroll through it. You can put this command in your ~/.tmux.conf if You want so it loads every time You run tmux.

Update: As of tmux 1.5 this option makes using the scroll wheel automatically switch to copy mode and scroll back the tmux scrollback buffer. It is not necessary to first hit Ctrl-B + [ any more. Scrolling back down to the prompt also ends copy mode automatically.

Pavlo
  • 4,231
154

There are some changes for Tmux 2.1

  • Mouse-mode has been rewritten. There's now no longer options for:

    • mouse-resize-pane
    • mouse-select-pane
    • mouse-select-window
    • mode-mouse

    Instead there is just one option: 'mouse' which turns on mouse support entirely.

That would be

set -g mouse on
# to enable mouse scroll, see https://github.com/tmux/tmux/issues/145#issuecomment-150736967
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'copy-mode -e'"
DannyRe
  • 1,651
77

The current version of tmux (1.5) lets you simply set the mode-mouse option on, and allows you to scroll with the mouse wheel.

         mode-mouse [on | off]
                 Mouse state in modes.  If on, the mouse may be used to
                 enter copy mode and copy a selection by dragging, to
                 enter copy mode and scroll with the mouse wheel, or to
                 select an option in choice mode.

In my .tmux.conf:

set-window-option -g mode-mouse on

update - as of tmux 2.1 this option has been renamed to simply mouse

jsbueno
  • 908
bukzor
  • 2,170
  • 2
  • 16
  • 22
77

Update:

Like others mentioned, in the newer tmux versions, you need to simply put the following in your .tmux.conf

setw -g mouse on

Previous recommendation for older tmux versions:

Try this in your .tmux.conf

# Make mouse useful in copy mode
setw -g mode-mouse on

Allow mouse to select which pane to use

set -g mouse-select-pane on

Allow xterm titles in terminal window, terminal scrolling with scrollbar, and setting overrides of C-Up, C-Down, C-Left, C-Right

set -g terminal-overrides "xterm*:XT:smcup@:rmcup@:kUP5=\eOA:kDN5=\eOB:kLFT5=\eOD:kRIT5=\eOC"

Scroll History

set -g history-limit 30000

Set ability to capture on start and restore on exit window data when running an application

setw -g alternate-screen on

Lower escape timing from 500ms to 50ms for quicker response to scroll-buffer access.

set -s escape-time 50

Taken from http://brainscraps.wikia.com/wiki/Extreme_Multitasking_with_tmux_and_PuTTY

18

Someone (from a source I lost) suggested adding the following to ~.tmux.conf:

set -g terminal-overrides 'xterm*:smcup@:rmcup@'

I have no idea how it works or what it does, but this now allows me to scroll with the mouse wheel inside a tmux session without having to enter tmux's copy mode; I just scroll the wheel and BAM! it works. Note that I'm using terminal.app, but I remember the OP gave the answer specifically for use with gnome-terminal.

tshepang
  • 3,341
Jed Daniels
  • 1,284
15

If you are already in a tmux session you can run the command

set mouse on

Reminder: to run commands, use your prefix then :.

Robert
  • 279
8

tmux 2.1 introduces new mouse binds.

I wrote these binds just now today. It seamlessly binds mouse wheel to arrows when not in Vim, because Vim is capable of interpreting the raw mouse wheel codes (for purposes such as choosing which Vim window to scroll for you depending on which one your mouse is over).

This means we can finally use the mouse only to view multiple man pages and whatever else accepts arrow keys. You may extend and chain the if logic as necessary to implement more logic for your applications.

bind -n WheelUpPane if "[[ #{pane_current_command} =~ vim ]]" "select-pane -t = ; send-keys -M" "select-pane -t = ; send-keys Up"
bind -n WheelDownPane if "[[ #{pane_current_command} =~ vim ]]" "select-pane -t = ; send-keys -M" "select-pane -t = ; send-keys Down"

With this new wheel binding capability it is possible to script the mouse wheel to do any context sensitive behavior that you like.

Steven Lu
  • 3,808
4

None of the other answers worked entirely for me in themselves. I had to enable the mouse option, using set -g mouse on and then used Shift + Mouse Wheel to scroll through the terminal.

Zoso
  • 213
  • 3
  • 13
4

The following worked fine for me even for a current session which I detached.

tmux set-option mouse on

After that attach to your session again.

tmux attach -t 0
3

Gnome-terminal does some neat trickery translating mouse scroll events to Up and Down arrow keys in conditions of restricted "usual" scrolling. For example, when you view some text using less (this happens in particular when you're reading a man), you can scroll the content using j, k, and arrow keys. But also, with gnome-terminal, you can do that with mouse scrolling, thanks to the mentioned trick.

So I guess tmux does some "capturing" of the terminal just like less - and the same mechanism in gnome-terminal kicks in: mouse scrolling translates into Up/Down arrow key presses.

You can turn this feature off in profile settings and get the regular scrolling in any circumstances. Just unmark the last checkbox in the "scrolling" tab: the checkbox.

ulidtko
  • 3,086
1

https://github.com/NHDaly/tmux-better-mouse-mode

Configurable and feature-rich implementation of mouse control for newer tmuxes. highly recommended.

You likely want to use

set -g @emulate-scroll-for-no-mouse-alternate-buffer "on"

with it also.

Steven Lu
  • 3,808
1

With mouse mode on and mapping the mouse wheel with Up/Down keys works pretty nice. Note: Reverse bindings if you don't like natural scroll.

set -g mouse on
bind -n WheelUpPane if -t = "test $(echo #{pane_current_command} |grep -e 'vim' -e 'man' -e 'less')" "select-pane -t = ; send-keys Up Up Up"  "if-shell -F -t = '#{?mouse_any_flag,1,#{pane_in_mode}}' 'send-keys -M' 'select-pane -t = ; copy-mode -e; send-keys -M'"
bind -n WheelDownPane if -t = "test $(echo #{pane_current_command} |grep -e 'vim' -e 'man' -e 'less')" "select-pane -t = ; send-keys Down Down Down"  "if-shell -F -t = '#{?mouse_any_flag,1,#{pane_in_mode}}' 'send-keys -M' 'select-pane -t = ; copy-mode -e; send-keys -M'"
Liv
  • 11
0

I'm using Warp, and I solved this issue with

Warp Settings > Features > Enable Mouse Reporting.

tanaydin
  • 101
-1

You can combine the binding ideas from the other answers to get a pretty satisfying scrolling behavior: works in vim changes to copy mode automatically in terminal and exits it when you reach the bottom still allows you to use your mousewheel in man, less and journalctl.

My code:

bind -n WheelUpPane if -t = "test $(echo #{pane_current_command} |grep -e 'man' -e 'less' -e 'journalctl')" "select-pane -t = ; send-keys Up Up Up Up"  "if-shell -F -t = '#{?mouse_any_flag,1,#{pane_in_mode}}' 'send-keys -M' 'select-pane -t = ; copy-mode -e; send-keys -M'"
bind -n WheelDownPane if -t = "test $(echo #{pane_current_command} |grep -e 'man' -e 'less' -e 'journalctl')" "select-pane -t = ; send-keys Down Down Down Down"  "if-shell -F -t = '#{?mouse_any_flag,1,#{pane_in_mode}}' 'send-keys -M' 'select-pane -t = ; copy-mode -e; send-keys -M'"

you can add other commands that require arrow keys for scrolling in the grep -e 'man' part

i added the send-keys multiple times, so one tick on the mousewheel will scroll 4 lines at a time

stealz
  • 1