Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

bind-key p select-pane -t 2 \; split-window 'xsel -i -b' \; paste-buffer

which gets me fairly close, all I have to do is hit control-d after I do prefix-p.

I tried fixing that by doing

bind-key p select-pane -t 2 \; split-window 'xsel -i -b << HERE\; tmux paste-buffer\; echo HERE'

But that just doesn't work. In fact if I pair this down to just

bind-key p select-pane -t 2 \; split-window 'xsel -i -b << HERE'

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 inst 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.

share|improve this question

12 Answers 12

up vote 15 down vote accepted

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)?

I suggest to write the buffer contents to a temporary file. Untested:

bind-key p save-buffer ~/.tmux-buffer \; run-shell "xsel -i -b <~/.tmux-buffer && rm ~/.tmux-buffer"

There's a race condition if you use this command in two separate tmux instances. I don't know how to solve this.

share|improve this answer
    
Thanks works perfectly, the race condition isn't a concern for me, I only use 1 session. –  Digital Powers Jun 27 '11 at 20:53

You might want to try tmux-yank plugin. It enables copying straight to system clipboard (OS X and Linux) and some other goodies like yanking the current shell line.

share|improve this answer

Sorry I didn't have enough reputation to comment. 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.

    bind-key -t vi-copy 'v' begin-selection
    bind-key -t vi-copy 'y' copy-pipe "xclip -sel clip -i"
    bind-key ] run-shell "xclip -sel clip -o | tmux load-buffer - ; tmux paste-buffer" 

Some notes:

  • for copy-pipe, I needed to specify my clipboard with -sel clip -i. That line combines the yank to tmux buffer and sends it to xclip.
  • to actually paste it, I needed to again specify which clipboard with -sel clip -o

This works beautifully now for my vim/zsh/tmux setup.

share|improve this answer
    
thank you! -sel clip -i solves the problem. Although, I do not understand why you need to specify clipboard again to paste it (standard ] works for me) –  antonk Jun 11 at 11:39

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 %%.

# Copy into clipboard
bind C-c choose-buffer "run \"tmux save-buffer -b %% - | xclip -i -selection clipboard\""
# Paste from clipboard
bind C-v run "xclip -o -selection clipboard | tmux load-buffer - ; tmux paste-buffer"
# Copy into primary
bind C-y choose-buffer "run \"tmux save-buffer -b %% - | xclip -i \""
# Paste from primary
bind C-p run "xclip -o | tmux load-buffer - ; tmux paste-buffer"

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.

# Copy into clipboard
bind C-c choose-buffer "run \"tmux save-buffer -b '%%' - | xsel -i -b\""
# Paste from clipboard
bind C-v run "xsel -o -b | tmux load-buffer - && tmux paste-buffer"
# Copy into primary
bind C-y choose-buffer "run \"tmux save-buffer -b '%%' - | xsel -i\""
# Paste from primary
bind C-p run "xsel -o | tmux load-buffer - && tmux paste-buffer"
share|improve this answer

The copy-pipe command was added as of version 1.8. It copies a selection to the tmux buffer and pipes it to a command. Now there is no need for a prefix binding. Here's an example:

bind -t vi-copy y copy-pipe "xclip"

# For OS X users:
bind -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"

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.

share|improve this answer
    
Works perfectly and is the simplest solution of the bunch. –  Naseer Jun 3 at 18:05
    
Doesn't work for me under Linux, this one does though: unix.stackexchange.com/a/131074/13601 –  Sebastian Blask Jul 2 at 15:45

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.

1 -- Would be ideal, but is non-working (due to tmux limitation?)

unbind-key -t emacs-copy M-w
bind-key -t emacs-copy M-w copy-selection\; \
  run-shell "tmux save-buffer - | xsel --input --clipboard"

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)

bind-key -n C-M-w send-keys M-w\; \
 run-shell "tmux save-buffer - | xsel --input --clipboard" 
share|improve this answer

This should work:

# move x clipboard into tmux paste buffer
bind C-p run "tmux set-buffer \"$(xclip -o)\"; tmux paste-buffer"
# move tmux copy buffer into x clipboard
bind C-y run "tmux save-buffer - | xclip -i"
share|improve this answer
1  
So it does. That's much nicer than using a temporary file, thanks. It even automatically copes with multiple concurrent sessions, thanks to the TMUX environment variables. –  Gilles Jul 10 '11 at 16:05
14  
A better instruction for the C-p would be: bind C-p run "xclip -o | tmux load-buffer - ; tmux paste-buffer" -- no quoting issues or command line limit issues. –  The Doctor What Feb 23 '12 at 3:37
    
# move x clipboard into tmux paste buffer bind C-p run "tmux set-buffer \\"$(xclip -o)\\"; tmux paste-buffer" # # move tmux copy buffer into x clipboard bind C-y run "tmux show-buffer | xclip -i -selection clipboard,primary" –  Bryan Hunt May 18 '12 at 10:16
6  
Does tmux have a hook to copy the selection into the clipboard when in copy-mode? –  Ehtesh Choudhury Feb 5 '13 at 18:56
    
xclip -o option didn't worked for me so I've changed to bind C-c run "tmux save-buffer - | xclip -selection c"\; display-message "Buffer copied to clipboard" –  Pawka Aug 23 '13 at 12:46

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.

On tmux 1.6 I tried this:

bind [ copy-mode
bind -t vi-copy ] run "tmux send-keys Enter; tmux show-buffer | xclip -i -selection clipboard;"
bind -t vi-copy C-] run "tmux send-keys Enter; tmux show-buffer | perl -pe 's/\n//' | xclip -i -selection clipboard;"

but that didn't work because tmux doesn't seem happy to bind complex things to the vi-copy table. So I ended up doing this instead:

bind [ copy-mode \;                                                        \
    bind -n ] run                                                          \
    "tmux send-keys Enter;                                                 \
     tmux show-buffer | xclip -i -selection clipboard;                     \
     tmux unbind -n C-];                                                   \
     tmux unbind -n ]" \;                                                  \
    bind -n C-] run                                                        \
    "tmux send-keys Enter;                                                 \
     tmux show-buffer | perl -pe 's/\n//' | xclip -i -selection clipboard; \
     tmux unbind -n C-];                                                   \
     tmux unbind -n ]"

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.

share|improve this answer
    
stackoverflow.com/a/10954274/69663 notes that there's a related bug report on the limitations of bind-key at sourceforge.net/tracker/… –  unhammer Nov 22 '12 at 13:12

After trying different solution, here is my final setup:

# move x clipboard into tmux paste buffer
bind C-p run "xclip -o | tmux load-buffer -
# move tmux copy buffer into x clipboard
bind C-y run "tmux save-buffer - | xclip -i -selection clipboard"

using save-buffer instead of show-buffer gives the real data without the data as shown with line-breaks when you are in splits.

I also use this to begin selection/copy in tmux copy-mode:

bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection

I guess it could be possible to merge the copy-selection with the 'y' key AND move it to the x clipboard.

share|improve this answer
    
I was trying to merge both commands as you mentioned with no success. Any ideas on how to do it? –  Rafael Vega Jun 6 '13 at 22:25

A simpler solution for Mac OS-X

This builds off of Alex's answer and uses stuff that didn't exist at the time.

If you are using homebrew (and if you aren't, why aren't you?) then you can just do this:

brew install reattach-to-user-namespace

Then in your ~/.tmux.conf:

set-option -g default-command "reattach-to-user-namespace -l zsh" # or bash...
bind C-c run "tmux save-buffer - | reattach-to-user-namespace pbcopy"
bind C-v run "reattach-to-user-namespace pbpaste | tmux load-buffer - && tmux paste-buffer"

To copy the buffer from tmux (you should have already copied something using tmux) into the Mac pasteboard, use <tmux-key> Control-c. It will now be available for Command-v or pbpaste

To paste something from the Mac pasteboard, use <tmux-key> Control-v.

share|improve this answer
2  
This answer is superb. I was using tmux show-buffer which caused line wrap issues tmux save-buffer - solved it. –  event_jr Mar 18 '12 at 5:00
    
What does the - at the end of tmux save-buffer - do? –  hekevintran Apr 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. –  The Doctor What Apr 15 '12 at 4:54
    
This worked for me. Thanks! –  program247365 Oct 9 '12 at 16:21
    
This answer cannot be +1'd enough. –  Christopher Jan 9 '13 at 18:49

A solution for Mac OS X. Follow these 2 steps:

  1. Use Chris Johnsen's workaround: https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard

  2. Add this code to your tmux config (~/.tmux.conf)

# paste clipboard into tmux buffer
bind v run-shell "reattach-to-user-namespace -l zsh -c 'tmux set-buffer \\"$(pbpaste)\\"; tmux paste-buffer'"
# put tmux buffer into x clipboard
bind y run-shell "reattach-to-user-namespace -l zsh -c 'tmux show-buffer | pbcopy'"

share|improve this answer

There is a solution on the Arch Wiki using xclip:

# move tmux copy buffer into x clipboard
bind-key C-y save-buffer /tmp/tmux-buffer \; run-shell "cat /tmp/tmux-buffer | xclip"

https://wiki.archlinux.org/index.php/Tmux

share|improve this answer
    
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. –  Gilles Jun 27 '11 at 21:09
    
Thanks Gilles. I'll update the wiki to reflect your points. –  jasonwryan Jun 27 '11 at 21:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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