iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📜

Fixing the Issue Where Scrolling Resets After Mouse Selection in tmux

に公開
2026/03/22

Problem

When mouse operation is enabled in tmux (set -g mouse on), selecting and copying text with the mouse while scrolling back to view past output resets the scroll position to the bottom.

This makes it inconvenient when you need to copy text while reviewing long logs or build outputs, as you lose your place.

Solution

Add the following to your ~/.tmux.conf:

~/.tmux.conf
# Enable mouse operation
set -g mouse on

# Select only on mouse drag end (do not copy, do not exit copy mode)
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X stop-selection

# Copy with Ctrl+C
#   While scrolling: copy-pipe (maintain copy mode, keep scroll position)
#   Not scrolling: copy-pipe-and-cancel (exit copy mode to resume typing)
bind-key -T copy-mode-vi C-c if-shell "[ #{scroll_position} -gt 0 ] 2>/dev/null" "send-keys -X copy-pipe" "send-keys -X copy-pipe-and-cancel"

# Automatically exit copy mode when reaching the bottom using mouse wheel down
bind-key -T copy-mode-vi WheelDownPane select-pane \; send-keys -X -N 5 scroll-down \; if-shell "[ #{scroll_position} -eq 0 ] 2>/dev/null" "send-keys -X cancel" ""

Apply the settings:

tmux source-file ~/.tmux.conf

This results in the following behavior:

Operation Behavior
Select with mouse drag Text is only selected (not copied)
Ctrl+C (while scrolling) Copy, keep scroll position. Exit normal mode by pressing q or scrolling to the bottom
Ctrl+C (no scroll) Copy, exit copy mode to resume input

If you are using byobu, add the following to ~/.byobu/.tmux.conf to enable these settings via byobu as well.

~/.byobu/.tmux.conf
source-file ~/.tmux.conf

The following section explains how these settings work in detail.

Cause

In the default tmux configuration, copy-pipe-and-cancel is executed at the end of a mouse drag (MouseDragEnd1Pane).

bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel

Because of and-cancel, copy mode is terminated, and the pane view returns to the bottom (the standard shell input position).

Why simply using copy-pipe is not enough

Changing it to copy-pipe (without cancel) would keep you in copy mode and preserve the scroll position.

bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe

However, this has a side effect: you will remain in copy mode even when selecting and copying text on the screen without scrolling. Since key inputs are interpreted as copy mode commands while in copy mode, you will not be able to type normally.

Situation copy-pipe-and-cancel (default) copy-pipe
Copy while scrolling back Scroll position is reset Scroll position is kept
Copy without scroll No problem Stays in copy mode; cannot type

Why stop-selection was used instead of conditional branching in MouseDragEnd1Pane

Previous configuration:

# Old: Conditional branching in MouseDragEnd1Pane (unstable in tmux 3.6a)
bind-key -T copy-mode-vi MouseDragEnd1Pane if-shell -F "#{?#{scroll_position},1,}" "send-keys -X copy-pipe" "send-keys -X copy-pipe-and-cancel"

This method had two issues:

  1. The 0 problem in if-shell -F: Because 0 is treated as false with the -F flag, it cannot distinguish between when #{scroll_position} is 0 (bottom) and when it is an empty string (outside copy mode). I used the ternary operator #{?#{scroll_position},1,} as a workaround, but the evaluation timing of scroll_position during mouse drags changed in tmux 3.6a, causing cases where it could not be judged correctly.

  2. Selection and copy happen simultaneously: Because it is copied immediately at the end of the drag, you cannot confirm the selection range before intentionally copying.

Therefore, in the new approach, I separated "selection" and "copying":

  • MouseDragEnd1Pane → stop-selection (selection only, no copy)
  • Ctrl+C → Execute copy (branching whether to maintain/exit copy mode depending on the scroll state)

At the point of pressing Ctrl+C, scroll_position is stable, so the conditional branch works correctly.

Explanation of settings

bind-key -T copy-mode-vi

bind-key is the command used to set key bindings (mapping operations to actions).

-T specifies the table. tmux has multiple key binding tables.

Table When it is used
prefix After pressing the prefix key (e.g., Ctrl+a)
root During normal operations
copy-mode-vi While in copy mode (when using vi key bindings)

By setting the binding for the copy-mode-vi table, we override the default behavior during copy mode.

MouseDragEnd1Pane

This is an event that signifies "left mouse button drag finished (button released) on the pane." It fires the moment you finish selecting text with the mouse.

If set -g mouse on is enabled, tmux automatically enters copy mode and starts text selection when you drag the mouse. This is the default behavior of tmux.

stop-selection

This is a command that only fixes the selection range without copying or exiting copy mode. The selected text remains highlighted, and you can copy it later using Ctrl+C or another command.

if-shell (shell command version)

if-shell is a command for conditional branching. When used without the -F flag, it determines the result based on the exit code of the shell command.

if-shell "shell command" "command on success (exit 0)" "command on failure"

In this setting:

if-shell "[ #{scroll_position} -gt 0 ] 2>/dev/null"
  • #{scroll_position} is expanded by tmux into an actual numerical value before execution
  • [ ... -gt 0 ] performs numerical comparison in the shell
  • While scrolling (1 or more) → Success (exit 0) → copy-pipe (maintains copy mode)
  • At the bottom (0) → Failure → copy-pipe-and-cancel (exits copy mode)
  • 2>/dev/null suppresses errors if scroll_position is expanded to an empty string outside of copy mode

Unlike format judging with the -F flag, the shell's -gt operator can correctly compare 0 as a number, allowing for reliable distinction between the "bottom (0)" and "scrolling (1 or more)."

copy-pipe and copy-pipe-and-cancel

Command Copy Copy mode
copy-pipe Do Maintain
copy-pipe-and-cancel Do Terminate

send-keys -X

-X is a flag that sends commands specific to copy mode. Since copy-pipe and others are copy mode commands, -X is required.

Operation after copying while scrolling back

Since you remain in copy mode after copying, you can return to normal mode in one of the following ways:

Operation Behavior
Press q Immediately exit copy mode, return to the bottom
Return to the bottom with mouse wheel tmux automatically exits copy mode when reaching the bottom

Automatic copy mode termination when reaching the bottom with the mouse wheel is not a default tmux behavior, so it is achieved with a custom WheelDownPane binding.

If you are using byobu

byobu is a wrapper for tmux, and its configuration file loading destination is different.

  • Via byobu → loads ~/.byobu/.tmux.conf (does not load ~/.tmux.conf)
  • tmux alone → loads ~/.tmux.conf

If you want to unify settings for both, write the settings in ~/.tmux.conf and source it from ~/.byobu/.tmux.conf.

~/.byobu/.tmux.conf
source-file ~/.tmux.conf

Verification Environment

  • tmux 3.6a
  • Windows Terminal
  • WSL2 (Ubuntu)

Discussion