189

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

5 Answers 5

206

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

7
  • 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... Dec 31, 2011 at 16:58
  • 10
    xsel: Can't open display: (null) : Inappropriate ioctl for device
    – itsazzad
    Dec 24, 2015 at 10:16
  • I solved the "Can't open display" problem on Windows by installing Xming and setting DISPLAY=:0
    – scottgwald
    Dec 25, 2017 at 4:49
  • 1
    For tmux users, it's more like Ctrl-b, [ -> go to position -> Space -> select texts -> Enter and paste by Ctrl-b, ]
    – elquimista
    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.
    – Benjamin R
    Jan 17, 2019 at 17:49
11

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
5
  • 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, 2015 at 7:53
  • 2
    You're right it does seem like useless cat abuse :-)
    – Erik
    May 25, 2015 at 10:44
  • 3
    if 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
    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?
    – n1k31t4
    May 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.
    – Benjamin R
    Jan 17, 2019 at 17:53
8

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.

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
4
  • But why would you even have this script on OS X where it's a part of the native userland?
    – Hultner
    May 21, 2015 at 8:02
  • * as a reference for similar functionality, * cross-platform dotfiles: github.com/westurner/dotfiles/blob/develop/scripts/pbcopy
    – Wes Turner
    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.
    – Hultner
    May 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 some type -P/ has variable assignments and /bin/test execs.
    – Wes Turner
    Dec 13, 2015 at 20:16
5

In WSL2, you have clipcopy and clippaste which are equivalent to pbcopy and pbpaste.

1
  • 1
    exactly what I needed thanks! Jun 17, 2022 at 3:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .