Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In a terminal in OSX I can pipe output to pbcopy and then go into a web browser and paste it. I tried this in Linux with xcopy but when I switch to the browser it just overwrites the clipboard with with whatever was in it the last time the browser was used. What works like pbcopy in Linux?

share|improve this question
1  
...and for those that don't know what pbcopy is, here's the man page developer.apple.com/library/mac/#documentation/Darwin/Reference/… –  Rob Cowell May 25 '11 at 9:48
    
+1 for my new favorite tool! –  Chris Nava May 25 '11 at 14:09

3 Answers 3

up vote 64 down vote accepted

if you have X installed you may try xsel in this way :

alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'

or with xclip :

alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'

now you can use'em :

echo 'go to my clipboard' | pbcopy

when I don't have X I use GNU Screen functionality to copy between open shells in a session using keyboard

to copy : Ctrl-a -> Esc -> go to wanted position * -> Space (to begin selecting) -> press k to go forward mark text -> Enter

to paste : Ctrl-a + ]

* movements are done with vim like key bindings (j, k, l & m).

share|improve this answer
1  
Thanks this is exactly the information I wanted. Tho now I wonder if there is a way I could make Screen let me use Emacs commands to select the area I want to copy... –  Noah Sussman Dec 31 '11 at 16:58

Put a script like this called pbcopy in your bin folder:

#!/bin/bash
xclip -i -sel c -f |xclip -i -sel p

This will put STDIN in both your selection buffer and clipboard:

echo Hello world |pbcopy
share|improve this answer
    
Why would the cat be needed? Won't it just redirect stdin to stdout in this case thus useless cat abuse or am I missing something? –  Hultner May 21 at 7:53
    
You're right it does seem like useless cat abuse :-) –  Erik May 25 at 10:44

To expand on the solutions of @Erik and @xpixelz; these two scripts should work on both platforms:

pbcopy:

#!/bin/bash
__IS_MAC=${__IS_MAC:-$(test $(uname -s) == "Darwin" && echo 'true')}
if [ -n "${__IS_MAC}" ]; then
  cat | /usr/bin/pbcopy
else
  # copy to selection buffer AND clipboard
  cat | xclip -i -sel c -f | xclip -i -sel p
fi

pbpaste:

#!/bin/bash
__IS_MAC=${__IS_MAC:-$(test $(uname -s) == "Darwin" && echo 'true')}
if [ -n "${__IS_MAC}" ]; then
  /usr/bin/pbpaste
else
  xclip -selection clipboard -o
fi
share|improve this answer
    
But why would you even have this script on OS X where it's a part of the native userland? –  Hultner May 21 at 8:02
    
* as a reference for similar functionality, * cross-platform dotfiles: github.com/westurner/dotfiles/blob/develop/scripts/pbcopy –  Wes Turner May 27 at 17:41
    
Still cat abuse and why not check if xclip exist instead and assign an alias if it does? Or check if the pbpaste binary exist would also be an option. Checking the uname seems like an odd approach to the problem. –  Hultner May 29 at 15:19

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.