How do I copy / paste from the system clipboard in Tmux in xterm on Linux?
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.
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
.