Is it possible to pipe to/from the clipboard in Bash?
Whether it is piping to/from a device handle or using an auxiliary application, I can't find anything.
For example, if /dev/clip
was a device linking to the clipboard we could do:
cat /dev/clip # Dump the contents of the clipboard
cat foo > /dev/clip # Dump the contents of "foo" into the clipboard
Make sure you are using alias
xclip="xclip -selection c"
otherwise you can't just use to Ctrl+v to paste it back in a different place.Ctrl+v
=== test
For Mac only:
These are located
/usr/bin/pbcopy
and/usr/bin/pbpaste
.On the Windows subsystem for Linux you can copy to the clipboard with clip.exe.
Keep in mind to use the
|
pipe command. And not a>
command, since that will not work.2018 answer
Use clipboard-cli. It works with macOS, Windows, Linux, OpenBSD, FreeBSD, and Android without any real issues.
Install it with:
npm install -g clipboard-cli
Then you can do
echo foo | clipboard
If you want, you can alias to
cb
by putting the following in your.bashrc
,.bash_profile
, or.zshrc
:alias cb=clipboard
There are a couple ways. Some of the ways that have been mentioned include (I think) tmux, screen, vim, emacs, and the shell. I don't know emacs or screen, so I'll go over the other three.
Tmux
While not an X selection, tmux has a copy mode accessible via
prefix-[
(prefix
is Ctrl+B by default). The buffer used for this mode is separate and exclusive to tmux, which opens up quite a few possibilities and makes it more versatile than the X selections in the right situations.To exit this mode, hit q; to navigate, use your
vim
oremacs
binding (default = vim), sohjkl
for movement,v/V/C-v
for character/line/block selection, etc. When you have your selection, hit Enter to copy and exit the mode.To paste from this buffer, use
prefix-]
.Shell
Any installation of
X11
seems to come with two programs by default:xclip
andxsel
(kinda like how it also comes with bothstartx
andxinit
). Most of the other answers mentionxclip
, and I really likexsel
for its brevity, so I'm going to coverxsel
.From xsel(1x):
And that's about all you need to know.
p
(or nothing) forPRIMARY
,s
forSECONDARY
,b
forCLIPBOARD
,o
for output.Example: say I want to copy the output of
foo
from a TTY and paste it to a webpage for a bug report. To do this, it would be ideal to copy to/from the TTY/X session. So the question becomes how do I access the clipboard from the TTY?For this example, we'll assume the X session is on display
:1
.Then I can
Ctrl-V
it into the form as per usual.Now say that someone on the support site gives me a command to run to fix the problem. It's complicated and long.
Pizza ordering seems like a productive use of the command line.
...moving on.
Vim
If compiled with
+clipboard
(This is important! Check yourvim --version
), Vim should have access to the XPRIMARY
andCLIPBOARD
selections. The two selections are accessible from the*
and+
registers, respectively, and may be written to and read from at your leisure the same as any other register. For example:If your copy of vim doesn't directly support access to X selections, though, it's not the end of the world. You can just use the
xsel
technique as described in the last section.Bind a couple key combos and you should be good.
Here is a ready to use bash script for reading the clipboard which works on multiple platforms. Please edit the script here if you add functionality (e.g. more platforms).