Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
tmux configuration, mouse copy & paste added
# My tmux configuration, partly based on https://github.com/wbkang/wbk-stow/blob/master/tmux-config/.tmux.conf
# Scroll History
set -g history-limit 50000
# show messages for 4 seconds instead
set -g display-time 4000
# set first window to index 1 (not 0) to map more to the keyboard layout
set-option -g renumber-windows on
set -g base-index 1
setw -g pane-base-index 1
# Make mouse useful, tmux > 2.1 include select, resize pane/window and console wheel scroll
set -g mouse on
# Lower escape timing from 500ms to 50ms for quicker response to scroll-buffer access.
set -s escape-time 50
## Clipboard integration
# ctrl+c to send to clipboard
bind C-c run "tmux save-buffer - | xclip -i -sel clipboard"
# ctrl+v to paste from clipboard
bind C-v run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer"
# Selection with mouse should copy to clipboard right away, in addition to the default action.
unbind -n -Tcopy-mode-vi MouseDragEnd1Pane
bind -Tcopy-mode-vi MouseDragEnd1Pane send -X copy-selection-and-cancel\; run "tmux save-buffer - | xclip -i -sel clipboard > /dev/null"
# Middle click to paste from the clipboard
unbind-key MouseDown2Pane
bind-key -n MouseDown2Pane run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer"
# Drag to re-order windows
bind-key -n MouseDrag1Status swap-window -t=
# Double click on the window list to open a new window
bind-key -n DoubleClick1Status new-window
@elijahgagne
Copy link

elijahgagne commented Jul 2, 2019

Thanks for sharing this. I borrowed lines 33-34 from you and changed them to:

unbind-key MouseDown2Pane
bind-key -n MouseDown2Pane run " \
  X=$(xclip -o -sel clipboard); \
  tmux set-buffer \"$X\"; \
  tmux paste-buffer -p; \
  tmux display-message 'pasted!' \
"

I was finding that when I pasted something that ended in a semicolon, the pasted content would be missing the semicolon. Above is preserving the semicolon for me.

@motherofperl
Copy link

motherofperl commented Aug 15, 2019

Thanks for this, finally mouse middle-click without shift works!

@kierun
Copy link

kierun commented Nov 8, 2019

It took me long time to find this… But at least that works!!!

Many thanks indeed.

@alissonbezerra
Copy link

alissonbezerra commented Dec 31, 2019

Copy and paste with mouse worked like a charm! Thanks for sharing.

@x3rAx
Copy link

x3rAx commented Feb 21, 2020

Hey @elijahgagne,

I could not get your improvement to work. When I copied something that ended in a semicolon, it would still eat the semicolon.

But thanks for the hint, who knows when this would have hit me ;)

While playing around, I also noticed, that when pasting something that starts with a minus (like --option) then it would paste the previous thing, whatever it was.

This led to the following solution:

bind-key -n MouseDown2Pane run "tmux set-buffer -- \"$(xclip -o -sel primary);\"; tmux paste-buffer -p"
                                                ^^                           ^                      ^^
                                                [1]                          [2]                    [3]

[1]: Stop parsing options. This prevents text starting with "-" to be interpreted as options.
[2]: Always insert a semicolon. The last semicolon is always cut away, so just add a dummy.
[3]: Paste bracket control codes. Allows for example pasting in vim normal mode.

@elijahgagne
Copy link

elijahgagne commented Feb 21, 2020

@x3rAx I agree my solution did not solve the semicolon issue. It always hits me pasting SQL statements in sqlplus. Thanks for sharing your update!

@stevencch99
Copy link

stevencch99 commented Jul 22, 2020

Thanks for sharing this. I borrowed lines 33-34 from you and changed them to:

unbind-key MouseDown2Pane
bind-key -n MouseDown2Pane run " \
  X=$(xclip -o -sel clipboard); \
  tmux set-buffer \"$X\"; \
  tmux paste-buffer -p; \
  tmux display-message 'pasted!' \
"

I was finding that when I pasted something that ended in a semicolon, the pasted content would be missing the semicolon. Above is preserving the semicolon for me.

It works for me on Ubuntu 20.04, thanks!

@chinglinwen
Copy link

chinglinwen commented Aug 6, 2020

thanks for sharing, finally found a way to restore right click to paste with mouse mode on with tmux version >2.1

# paste from right click
unbind-key MouseDown3Pane
bind-key -n MouseDown3Pane run " \
  X=$(xclip -o -sel clipboard); \
  tmux set-buffer \"$X\"; \
  tmux paste-buffer -p; \
  tmux display-message 'pasted!' \
"

PS: but I still can only make it paste from tmux's copy, not from other windows ( I'm using windows's chrome secure shell for ssh )

tried this too, same result with above

bind-key -n MouseDown3Pane run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer"

@deadlysyn
Copy link

deadlysyn commented Sep 16, 2020

tyvm! i'm probably odd, but prefer "unified clipboard" (whether ctrl-* or mouse, do the same) so also found it useful that xclip lets you specify multiple select args...

xclip -se c -se p -i -r

@RolandWarburton
Copy link

RolandWarburton commented Jan 4, 2021

Thank you for sharing this! Exactly solved my clipboard problems

@cygenb0ck
Copy link

cygenb0ck commented Mar 7, 2021

thank you for sharing!

@simonalsa
Copy link

simonalsa commented Mar 24, 2021

ty

@xiaolitongxue666
Copy link

xiaolitongxue666 commented Feb 16, 2022

In centos8 stream pre install xclip , this config work well , thanks a lot~!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment