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?
-
2...and for those that don't know what pbcopy is, here's the man page developer.apple.com/library/mac/#documentation/Darwin/Reference/…– Rob CowellMay 25, 2011 at 9:48
-
A tool that can be helpful with this is ClipIt (and similar clipboard managers). Not only it plays nicely in the terminal, it also preserves the clipboard history. Quite useful.– brandizziDec 10, 2016 at 18:57
5 Answers
If you have desktop version of Linux (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 them:
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.
Copy:
- Ctrl-a
- Esc
- go to wanted position *
- Space (to begin selecting)
- press k to go forward mark text
- Enter
Paste:
- Ctrl-a + ]
* movements are done with vim like key bindings (j, k, l & m).
-
1Thanks 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... Dec 31, 2011 at 16:58
-
10
-
I solved the "Can't open display" problem on Windows by installing Xming and setting DISPLAY=:0 Dec 25, 2017 at 4:49
-
1For tmux users, it's more like
Ctrl-b, [ -> go to position -> Space -> select texts -> Enter
and paste byCtrl-b, ]
Oct 14, 2018 at 6:13 -
Whenever possible—if this is something permanent you want, of course—rather than complicating your namespace with aliasing in your profile, it's going to be better to create a basic script for this in
/bin/
. Especially if you share your profile between macOS and linux machines. Jan 17, 2019 at 17:49
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
-
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?– HultnerMay 21, 2015 at 7:53 -
2
-
3if you're (like me) wondering about where is the mysterious "useless cat", it has been removed by the edit. NTS: in case of confusing comments, check the history ^^' Jan 31, 2017 at 16:40
-
How do you paste the contents after using this? Just using
xclip -o
? Or is there a better way?– n1k31t4May 4, 2017 at 5:23 -
@n1k31t4 What I did was make an equivalent script using
xclip -sel c -o
for a script at/bin/pbpaste
. Jan 17, 2019 at 17:53
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
-
But why would you even have this script on OS X where it's a part of the native userland?– HultnerMay 21, 2015 at 8:02
-
* as a reference for similar functionality, * cross-platform dotfiles: github.com/westurner/dotfiles/blob/develop/scripts/pbcopy May 27, 2015 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.– HultnerMay 29, 2015 at 15:19
-
2@Hultner "Checking the uname seems like an odd approach to the problem" You could also check for binaries, preferentially, in order, by exception with a
${a:-${b:-${c:+${d}}}}
and sometype -P
/has
variable assignments and /bin/test execs. Dec 13, 2015 at 20:16
This answer refers to the Linux Subsystem for Windows.
Short answer: use clip.exe
as if it were pbcopy
in order to put something on the Windows clipboard. It's magic. Example echo "Hello Windows" | clip.exe
in your bash or Ubuntu bash terminal, and then `ctrl-v' in a Windows program.
More context:
In a comment above I mentioned that, when using Xming on Windows to enable this functionality, it is necessary to set a DISPLAY variable (export DISPLAY=:0
, in many cases) before the xsel
and xclip
solutions work. Infuriatingly, this solution works in an unreliable, stochastic way -- when pasting from Linux to Windows, pressing ctrl-v
between one and ten times causes the clipboard to be pasted (one time) (this is on my Windows 10 Surface Book 2). Don't waste your time, use clip.exe
.
NOTE: Don't forget the .exe
. Otherwise Ubuntu bash will suggest that you install the Linux package geomview
, which is not what you want.
In WSL2, you have clipcopy
and clippaste
which are equivalent to pbcopy
and pbpaste
.