What's like OSX's pbcopy for Linux

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

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

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

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

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like