i Copy from tmux to system clipboard · Dark Matter Industries

Copy from tmux to system clipboard

When I was new to tmux many years ago, one of the initial things that kept me from adopting it fully was the inability (or so I thought) of copying from within a tmux session into my OS’s clipboard.

Of course, this is possible and it just takes a little know how.

To see how copy is bound in your tmux, we list out copy-mode-vi (I use vi but you can also use copy-mode-emacs).

 tmux -f /dev/null list-keys -T copy-mode-vi

Note: This actually creates a new tmux session but is placed into /dev/null then that session will list the keys to stdout. If you don’t add /dev/null you could get nested tmux sessions if you ran that command inside another tmux session (not recommended!). If you run it outside a tmux session, all you will get is a new empty tmux session!

To bind copy to pipe its contents into a macOS clipboard, add the following to your .tmux.conf:

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

What this does is this: when you have selected what to copy by dragging with the mouse, it copies the text highlighted, cancels the selection and pipes it into pbcopy (use xcopy for linux). Of course, you can bind other keys such as y for yank, but why not just have a simple one-drag operation?

Note that in iterm2 the ability to copy anything from within the terminal to the pasteboard is built in. Visit Preferences > General > Selection and check “Applications in terminal may access the clipboard”.