How to make the middle mouse button paste from the primary selection in tmux when mouse mode is enabled?

Viewed 1653

In most terminal emulators Middle Mouse Button will paste from the X Windows primary selection. In tmux with mouse mode enabled (set -g mouse on) Middle Mouse Button no longer pastes from the primary selection because the mouse click is captured by tmux rather than by the terminal emulator. To paste you have to do Shift + Middle Mouse Button to pass the click through to the terminal emulator.

How can I make Middle Mouse Button paste without having to hold down Shift, even when mouse mode is enabled?

2 Answers

Add this to your ~/.tmux.conf file:

# Make middle-mouse-click paste from the primary selection (without having to hold down Shift).
bind-key -n MouseDown2Pane run "tmux set-buffer -b primary_selection \"$(xsel -o)\"; tmux paste-buffer -b primary_selection; tmux delete-buffer -b primary_selection"

Requires xsel to be installed. (sudo apt install xsel on Ubuntu.)

Explanation

Adds a Middle Mouse Button (MouseDown2Pane) binding that:

  1. Uses tmux set-buffer to load the output of the xsel -o command (i.e. the contents of the primary selection) into a temporary tmux paste buffer named primary_selection

  2. Uses tmux paste-buffer to paste the primary_selection buffer into the current pane

  3. Uses tmux delete-buffer to delete the primary_selection buffer. This is to avoid messing with your buffers. tmux's paste command (Ctrl + b ] by default) pastes from the most-recently-created paste buffer. We don't want the primary_selection buffer to become your most-recently-created buffer and replace the previous buffer for the Ctrl + b ] binding. Nor do we want the primary_selection buffer to appear in your buffers list. So we delete it

In tmux 3.2, this change was made.

CHANGES FROM 3.1c TO 3.2

  • Add a default binding for button 2 to paste.
Related