tmux trick #2: copy to clipboard
This post is part of a series
Recently, for reasons that I may explain in a future blog post, I went
through some old configuration files of mine, and I found something in
my .tmux.conf that I thought would be worth a second post in this
series - only 2 years after the first episode, not bad!
Copy mode
tmux has a feature called copy mode
that allows one to visually select and copy text, all via your keyboard.
By default you can enter copy mode by pressing C-b [ (Ctrl+B followed
by [); from there you can navigate the text that is currently on your
terminal with arrow keys and Emacs-style
key bindings - or with hjkl and other
Vim-style bindings if
you have a VISUAL or EDITOR environment
variable set to vi.
You can then start selecting text by pressing Space, and confirm the
selection by pressing Enter. The selection will then be copied to
an internal tmux buffer, and you can paste it by pressing C-b ].
Actually, tmux offers multiple copy-buffers, but honestly I have never used this feature. You can read more about this in its manual page.
Copy to clipboard
By default, tmux will copy the selection to its internal buffer, but you
may want to paste that text somewhere outside of tmux - maybe in a chat
application or in a web browser URL bar. And here is the trick: you can
actually tell tmux to pipe
the selection to a custom command. For example, if you have this in your
.tmux.conf:
set -s copy-command "xsel -ib"
tmux will not only copy the selection to its internal buffer, but also
send it to the xsel -ib command. In case you did not know,
xsel
is a command that copies its standard input to the X session
clipboard;
this way you will be able to paste the copied text into any other
graphical application with the usual Ctrl+V. Neat!
tmux show-buffer
Here one last trick: with the tmux show-buffer command you can print the
current tmux selection to standard output. You can use this in shell scripts,
for example, or in more complex tmux key bindings in your configuration file.
And that’s a teaser for the next tmux trick :)