8

I'd like to copy into and paste from the system (X11) clipboard when making a selection in Tmux and I'd like to bind these actions to the mouse; left click for copy and middle click for paste.

7

For older versions of Tmux (<1.5) or other systems, try tmux-yank. For this specific case, however, Tmux very nicely integrates with the system.

In your ~/.tmux.conf, add:

set -g mouse on
set -g set-clipboard external
bind -T root MouseUp2Pane paste

to enable mouse support, copying to the system clipboard, and bind a middle-click on a pane to paste.

And in your ~/.Xresources:

xterm*selectToClipboard: true
xterm*disallowedWindowOps: 20,21,SetXProp

to let Xterm select to the system clipboard as well, and allow Tmux to modify the clipboard (a "window operation").

Then, to apply the changes to your ~/.Xresources, run xrdb -merge ~/.Xresources and restart Xterm and Tmux.


To support macOS and Windows Subsystem for Linux (WSL), we can add

run-shell $HOME/.tmux.conf.sh

to the ~/.tmux.conf, and then create ~/.tmux.conf.sh with the following contents:

#!/bin/bash

bind_copy=(bind-key -T copy-mode-vi MouseDragEnd1Pane)

# `tmux_bind_copy pbcopy` will make selecting with the mouse (and then
# releasing the selection) in tmux pipe the selection to `pbcopy`
function tmux_bind_copy {
        tmux "${bind_copy[@]}" send-keys -X copy-pipe-and-cancel "$@"
}

if [[ "$(uname)" == "Darwin" ]]
then
    # Copy with pbcopy on macOS
    tmux_bind_copy pbcopy
fi

if [[ ! -z "$WSL_DISTRO_NAME" ]]
then
    # copy with Windows' clip.exe on WSL
    tmux_bind_copy /mnt/c/Windows/System32/clip.exe
fi

Note that other “advanced” configuration choices can be made in .tmux.conf.sh, such as setting configuration values based on the current hostname, distribution, and so on; using if-shell is also an option, but is generally pretty clumsy in practice, so using a shell script is an accepted solution.

Also note the weird syntax "${bind_copy[@]}", which inserts the $bind_copy array without performing glob expansion.

| improve this answer | |
2

I'm using $tmux -Version tmux 2.8. Before copying to clipboard, the magic is to press shift in advance and draw the area first (which you wanna copy to system clipboard), the area gets grey colored text background.

Afterwards press the default hotkey to copy to system clipboard. I am using Konsole and there it is Shift-Ctrl-C.

| improve this answer | |
  • This depends on the terminal emulator you're using (like xterm or Konsole or alacritty), and not on tmux. It can also include (in some selection modes) things like window borders if you have horizontal splits. – 9999years Apr 19 at 15:31
  • 1
    this solution works for gnome-terminal + tmux the same way. – Hartmut P. Apr 20 at 17:21
  • it does not work on xterm + tmux – Hartmut P. Apr 20 at 17:23
  • 1
    Works on tmux 2.8 + ROXTerm + Debian Linux! – RayLuo Aug 7 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.