How can I copy the output of a command directly into my clipboard?
I've created a tool for Linux/OSX/Cygwin that is similar to some of these others but slightly unique. I call it cb
and it can be found in this github gist.
In that gist I demonstrate how to do copy and paste via commandline using Linux, macOS, and Cygwin.
Linux
_copy(){
cat | xclip -selection clipboard
}
_paste(){
xclip -selection clipboard -o
}
macOS
_copy(){
cat | pbcopy
}
_paste(){
pbpaste
}
Cygwin
_copy(){
cat > /dev/clipboard
}
_paste(){
cat /dev/clipboard
}
Note: I originally just intended to mention this in my comment to Bob Enohp's answer. But then I realized that I should add a README to my gist. Since the gist editor doesn't offer a Markdown preview I used the answer box here and after copy/pasting it to my gist thought, "I might as well submit the answer." If you would like to discuss functionality/bugs it would probably be best to do that in the comments for the gist on github.
cb
A leak-proof tee to the clipboard
This script is modeled after tee
(see man tee
).
It's like your normal copy and paste commands, but unified and able to sense when you want it to be chainable
Examples
Copy
$ date | cb
# clipboard contains: Tue Jan 24 23:00:00 EST 2017
Paste
# clipboard retained from the previous block
$ cb
Tue Jan 24 23:00:00 EST 2017
$ cb | cat
Tue Jan 24 23:00:00 EST 2017
$ cb > foo
$ cat foo
Tue Jan 24 23:00:00 EST 2017
Chaining
$ date | cb | tee updates.log
Tue Jan 24 23:11:11 EST 2017
$ cat updates.log
Tue Jan 24 23:11:11 EST 2017
# clipboard contains: Tue Jan 24 23:11:11 EST 2017
Copy via file redirect
(chronologically it made sense to demo this at the end)
# clipboard retained from the previous block
$ cb < foo
$ cb
Tue Jan 24 23:00:00 EST 2017
# note the minutes and seconds changed from 11 back to 00
I always wanted to do this and found a nice and easy way of doing it. I wrote down the complete procedure just in case anyone else needs it.
First install a 16 kB program called xclip
:
sudo apt-get install xclip
You can then pipe the output into xclip
to be copied into the clipboard:
cat file | xclip
To paste the text you just copied, you shall use:
xclip -o
To simplify life, you can set up an alias in your .bashrc file as I did:
alias "c=xclip"
alias "v=xclip -o"
To see how useful this is, imagine I want to open my current path in a new terminal window (there may be other ways of doing it like Ctrl+T on some systems, but this is just for illustration purposes):
Terminal 1:
pwd | c
Terminal 2:
cd `v`
Notice the ` `
around v
. This executes v
as a command first and then substitutes it in-place for cd
to use.
Only copy the content to the X
clipboard
cat file | xclip
If you want to paste somewhere else other than a X
application, try this one:
cat file | xclip -selection clipboard
On OS X, use pbcopy
; pbpaste
goes in the opposite direction.
pbcopy < .ssh/id_rsa.pub