What's like OSX's pbcopy for Linux

147

77

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?

tony_sid

Posted 2011-05-25T09:08:20.627

Reputation: 11 651

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.

– brandizzi – 2016-12-10T18:57:09.387

2

...and for those that don't know what pbcopy is, here's the man page http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/pbcopy.1.html

– Rob Cowell – 2011-05-25T09:48:13.497

Answers

170

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

amized

Posted 2011-05-25T09:08:20.627

Reputation: 2 017

8xsel: Can't open display: (null) : Inappropriate ioctl for device – itsazzad – 2015-12-24T10:16:30.577

I solved the "Can't open display" problem on Windows by installing Xming and setting DISPLAY=:0

– scottgwald – 2017-12-25T04:49:19.187

1For tmux users, it's more like Ctrl-b, [ -> go to position -> Space -> select texts -> Enter and paste by Ctrl-b, ] – elquimista – 2018-10-14T06:13:03.807

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. – Benjamin R – 2019-01-17T17:49:41.373

how to install xsel – Alexander Mills – 2019-09-27T18:56:23.440

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... – Noah Sussman – 2011-12-31T16:58:31.807

10

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

Erik

Posted 2011-05-25T09:08:20.627

Reputation: 255

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 – 2015-05-21T07:53:44.200

2You're right it does seem like useless cat abuse :-) – Erik – 2015-05-25T10:44:13.860

2if 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 ^^' – yaitloutou – 2017-01-31T16:40:18.710

How do you paste the contents after using this? Just using xclip -o ? Or is there a better way? – n1k31t4 – 2017-05-04T05:23:17.047

@n1k31t4 What I did was make an equivalent script using xclip -sel c -o for a script at /bin/pbpaste. – Benjamin R – 2019-01-17T17:53:49.097

5

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

Wes Turner

Posted 2011-05-25T09:08:20.627

Reputation: 204

But why would you even have this script on OS X where it's a part of the native userland? – Hultner – 2015-05-21T08:02:25.197

>

  • as a reference for similar functionality, * cross-platform dotfiles: https://github.com/westurner/dotfiles/blob/develop/scripts/pbcopy
  • – Wes Turner – 2015-05-27T17:41:33.907

    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 – 2015-05-29T15:19:16.263

    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 some type -P/ has variable assignments and /bin/test execs. – Wes Turner – 2015-12-13T20:16:01.527

    2

    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.

    scottgwald

    Posted 2011-05-25T09:08:20.627

    Reputation: 121