it doesn't do anything so I am assuming that split-window doesn't like << in a shell command.

Any ideas?

Edit:
You can skip the select-pane -t 2 if you want, it isn't really important. I just use a specific layout and pane 2 is the one I prefer to split when I doing something else so that goes into my bindings involving splits by default.

16 Answers
16

Here documents need newlines. For example, in a shell script, you can write

cat <<EOF >somefile; echo done
file contents
EOF

I don't think tmux lets you put newlines there, and even if it did, this wouldn't be a good approach. What if the data itself contains HERE alone on a line (e.g. because you're copying your .tmux.conf)?

Thanks works perfectly, the race condition isn't a concern for me, I only use 1 session.
– Digital PowersJun 27 '11 at 20:53

To avoid race condition, I'd suggest using mktemp which is widely available
– GriefJul 8 '16 at 19:40

@Grief Good idea. In this case, using the PID of the tmux process would work as well and would probably be easier to arrange ($PPID in a shell launched by tmux). But how do you get the variable file name (however it's constructed) into the argument of save-buffer? I guess save-buffer - | … like in Grauwolf's answer, which does away with a temporary file altogether?
– GillesJul 8 '16 at 19:44

You are correct, my fault, this would not be possible.
– GriefJul 8 '16 at 19:50

This answer is superb. I was using tmux show-buffer which caused line wrap issues tmux save-buffer - solved it.
– event_jrMar 18 '12 at 5:00

What does the - at the end of tmux save-buffer - do?
– hekevintranApr 13 '12 at 17:58

Emits the save-buffer to stdout. You can replace the - with a filename and it'll save it there. This prevents you from writing (and having to clean up) a file.
– docwhatApr 15 '12 at 4:54

1

As of OS X 10.10 reattach-to-user-namespace is no longer necessary. Almost every tutorial and documentation regarding tmux and OS X mentions it, because copy & pasting wasn’t easily possible a couple of years ago. These days it (mostly) works out of the box.
– Rafael BugajewskiAug 3 '15 at 8:01

Now when you have some text selected in copy-mode, y will copy that text to the tmux buffer as well as the OS clipboard. However, it doesn't help if you want to get text from the clipboard to the tmux buffer.

First a little history

When this question was first asked (June, 2011) copying text from Tmux to the clipboard had to be a two-step process, first copy the selected text to Tmux's buffer, then copy the text from Tmux's buffer to the system clipboard.

In 1.8 (March, 2013) the copy-pipe command was added which allows you to copy the selected text straight into the system clipboard (or other destination).

Then in 2.4 (April, 2017) the key tables were significantly changed rendering many of the answers on this page obsolete.

I dislike having extra key presses, so I wanted something more ambitious. Enter copy mode with [, highlight the right text, and then press ] to copy the text to the clipboard.

Also, tmux likes to include newlines in expressions that wrap (and oddly those newlines are in the wrong place! They aren't where the expression wrapped on the screen). So I wanted C-] to copy the expression removing the newlines.

This leads to weird things (] or C-] will act funny the next time you use them, but only once) if you exit from copy mode in another way besides ] or C-]. This could be fixed with some more bindings I imagine, but this does what I want most of the time.

Does tmux create the save-buffer file securely in a shared directory? Otherwise putting it in /tmp is a security hole. There's a useless use of cat, and the removal of the temporary file is missing. Then it's a matter of xsel vs xclip, which are equivalent here, and you have my (indenpendently written) solution.
– GillesJun 27 '11 at 21:09

A slightly modified variant of the accepted answer, which I cooked up.

I also prefer to use the system clipboard (Ctrl-C, Ctrl-V) in addition to xclip's primary default clipboard (the one you get with mouse selection and mouse middle-clicks). For choosing the system clipboard you need to pass -selection clipboard argument to xclip.

Also while copying a buffer into a clipboard, I like the ability to choose which buffer I want to copy. It is done using choose-buffer and then passing buffer the argument to save-buffer using -b %%.

UPDATE:
Somehow with tmux 1.8 and two of my openSUSE 12.2 installations, I see a hang when tmux run-shell is invoked with xclip reading from stdin over a pipe. I instead decided to give xsel a try and that seems to work. These are the equivalent key bindings using xsel. I also used && to chain the commands in paste for more robustness.

1. Irresponsive xclip

xclip could also be used for that purpose, unlike xsel it works better on printing raw bitstream that doesn't fit the current locale. Nevertheless, it is neater to use xsel instead of xclip, because xclip does not close STDOUT after it has read from tmux's buffer. As such, tmux doesn't know that the copy task has completed, and continues to wait for xclip's termination, thereby rendering tmux unresponsive. A workaround is to redirect STDOUT of xclip to /dev/null

@user, strange. There could be many sources of errors. Check first that your quoting is alright. Then, are you familiar with sed? The command should be: sed -n s/^DISPLAY=//p. Alternatively, you could use cut: tmux show-env DISPLAY | cut -d'=' -f2. It might also be worth checking your tmux version.
– PlasmaBinturongApr 25 at 8:40

I am trying to figure out a decent way to copy what I have in a tmux
buffer into my clipboard. I have tried a couple of different things
like

For emacs style bindings there are a few different options. (Vi-style should be similar?) The typical binding for doing "copy-selection" in emacs is M-w, so it would be very convenient to have the same binding in tmux.

2 -- Compromise that works well for me: Accept using a different keybinding for emacs-copy in tmux. Use for example C-M-w. (This binding which will be defined "globally" -- not only in copy-mode -- but only useful in copy-mode)

Many of these answers didn't work for my Ubuntu 14.04 on zsh as-is. Not sure if my shell had anything to do with it, but here is my working version of using this strategy for copy, and this strategy for paste.

This way there are no consequences if you exit from copy mode in another way besides ], the above code is wrapped in tundle/tmux-yank a tmux plugin for copying tmux text into the system clipboard, it targets tmux >= 1.6 on OS X and Linux.