How can I copy the output of a command directly into my clipboard?
Solution 1
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
Solution 2
On OS X, use pbcopy
; pbpaste
goes in the opposite direction.
pbcopy < .ssh/id_rsa.pub
Solution 3
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
Solution 4
I wrote this little script that takes the guess work out of the copy/paste commands.
The Linux version of the script relies on xclip being already installed in your system. The script is called clipboard.
#!/bin/bash
# Linux version
# Use this script to pipe in/out of the clipboard
#
# Usage: someapp | clipboard # Pipe someapp's output into clipboard
# clipboard | someapp # Pipe clipboard's content into someapp
#
if command -v xclip 1>/dev/null; then
if [[ -p /dev/stdin ]] ; then
# stdin is a pipe
# stdin -> clipboard
xclip -i -selection clipboard
else
# stdin is not a pipe
# clipboard -> stdout
xclip -o -selection clipboard
fi
else
echo "Remember to install xclip"
fi
The OS X version of the script relies on pbcopy and pbpaste which are preinstalled on all Macs.
#!/bin/bash
# OS X version
# Use this script to pipe in/out of the clipboard
#
# Usage: someapp | clipboard # Pipe someapp's output into clipboard
# clipboard | someapp # Pipe clipboard's content into someapp
#
if [[ -p /dev/stdin ]] ; then
# stdin is a pipe
# stdin -> clipboard
pbcopy
else
# stdin is not a pipe
# clipboard -> stdout
pbpaste
fi
Using the script is very simple since you simply pipe in or out of clipboard
as shown in these two examples.
$ cat file | clipboard
$ clipboard | less
Solution 5
Linux, macOS, Windows (WSL/CYGWIN)
Each of those systems use its own tool to incorporate the clipboard functionality into the command line interface (CLI). This means, when using for example the Ubuntu CLI on Windows Subsystem for Linux (WSL) the usual xclip
solution won't work. The same holds true for macOS.
The following table provides an overview for the copy/paste tools needed on the different systems:
OS | Copy | Paste |
---|---|---|
WSL | clip.exe |
powershell.exe Get-Clipboard |
CYGWIN | > /dev/clipboard |
cat /dev/clipboard |
macOS | pbcopy |
pbpaste |
Linux | xclip -sel clip |
xclip -sel clip -o |
Unified .bashrc
solution
Just put the following code into your ~/.bashrc
to enable the usage of copy
and paste
on all systems. The solution works on "normal" Linux systems (i.e. Ubuntu, Debian) as well as on WSL and macOS:
if grep -q -i microsoft /proc/version; then
# on WSL: version contains the string "microsoft"
alias copy="clip.exe"
alias paste="powershell.exe Get-Clipboard"
elif grep -q -i cygwin $(uname -a); then
# on CYGWIN: uname contains the string "cygwin"
alias copy="/dev/clipboard"
alias paste="cat /dev/clipboard"
elif [[ ! -r /proc/version ]]; then
# on MAC: version is not readable at all
alias copy="pbcopy"
alias paste="pbpaste"
else
# on "normal" linux
alias copy="xclip -sel clip"
alias paste="xclip -sel clip -o"
fi
Usage on ALL systems
To copy:
# pipe
echo "hello world" | copy
# or for direct file input
copy < file
To paste:
paste > file
Comments
-
Legend over 2 years
How can I pipe the output of a command into my clipboard and paste it back when using a terminal? For instance:
cat file | clipboard
-
khotyn about 13 years"cat file | xclip" only copy the content to the 'X' clipboard, if you want to paste somewhere else other than a 'X' application, try this one: "cat file | xclip -selection clipboard"
-
Matt Joiner about 13 yearsYour choice of handle is appropriate!
-
doublejosh about 13 yearsYups! Just like this for ex:
pbcopy < .ssh/id_rsa.pub
-
Anake almost 11 yearsIf you want to remove the newline character so that you can directly paste and edit the result of "pwd | c", you can do "pwd | tr -d '\n' | xclip -selection c"
-
StackedCrooked over 10 yearsI use the names
pbcopy
andpbpaste
on Linux so it works for both Linux and OS X. -
Ben over 9 yearsdoesn't work for command output tho - e.g.
pbcopy < git merge-base master some-branch
-
Hockey about 9 yearsTo make the script work globally in ubuntu: * Save it at
~/.scripts/clipboard
* Make it executablechmod +x ~/.scripts/clipboard
for bash: * addexport PATH=$PATH:~/.scripts
to the end of~/.bashrc
for fish: * addset PATH ~/.scripts $PATH
to~/.config/fish/fish.config
If any of the files or folders don't already exist just create them. -
Sebastian Nowak almost 9 yearsYou can also wrap this script as a function
clipboard(){ ... }
and paste it into .bashrc -
Atav32 almost 9 yearsif you're using tmux, it won't work unless you reattach your session: superuser.com/a/413233/146254
-
d.raev over 8 years@AfshinMoazami, the title and the question itself is generic, Only the tags hint for unix system, and my answer covers the case when you try to get the date FROM a unix server TO a Windows machine, which may be useful future readers.
-
Yibo Yang over 8 yearsalso define
alias "cs=xclip -selection clipboard"
andalias "vs=xclip -o -selection clipboard"
to make copying/pasting from system clipboard easier -
Champ about 8 years
xclip fileName
works on my Ubuntu Trusty 14.04. Also,xclip -selection c
works for normal copy of Control-C. The equivalent of Control-V isxclip -o -selection c
-
Nearoo almost 8 yearsWouldn't the syntax of aliasses be
alias a="b -c"
instead ofalias "a=b -c"
? That's what I read on ubuntuusers, but apparently noone here notices it... -
Bruno Bronosky over 7 yearsThis is very similar to the one I made, but I allow chaining like
date | cb | tee -a updates.log
. That would send thedate
command output to the clipboard and pass it along to thetee -a
command which appends it to a file and passes it along tostdout
. But it's like a "leak-proof tee to the clipboard" because if you just dodate | cb
you get no output. And finally it also doescb > file.txt
gist.github.com/RichardBronosky/… -
Ulf Aslak over 7 yearsGive a usage example please
-
Ivaylo Strandjev almost 7 yearsOne suggestion - strip the trailing newline character to avoid executing the command on pasting(e.g. if copy-pasting a path). I do that by modifying the command
xclip -i -selection clipboard
toxargs echo -n | xclip -i -selection clipboard
-
Ivaylo Strandjev almost 7 yearsActually the command above does not work well if the thing you want to copy to clipboard is multi line. Here is what I did to fix this:
cat - | perl -pe 'chomp if eof' | xclip -selection clipboard
-
flith almost 7 yearsUpvoted because yes, useful to Windows admins who only have Powershell to play with locally (although now with LSW actually functional, I can drop into bash directly from Powershell and do my *nixy things from there).
-
ren.rocks almost 7 yearsyou can also use cat, if you already work with it: cat file.txt | pbcopy
-
rogue lad almost 7 yearsIts not available in RHEL.
-
Admin over 5 yearsDo note that if an xserver (such as vcxsvr or xming) is installed on windows, a display is up and xclipboard is running, you can actually use xclip seemlessly.
-
Admin over 5 yearsIts working on DOS prompt as well as git bash on windows 10 i.e.
echo Hello World | clip
-
alper over 5 yearsI am not sure but I am getting following error
Error: Can't open display: (null)
@Legend -
imrek over 4 years
cat file | ...
There we go again. -
Sam over 4 yearsI know the comment is 5 years old, but in case someone stumbles on to this in the future, to use with command output, do
git merge-base master some-branch | pbcopy
-
Joshua Dyck about 4 yearsThis worked perfect for me to copy a command output to the clipboard in terminal on macOS, thanks!
-
Pablo Bianchi over 3 years
-i
is the default. I use a lot for egxsel -b | sort | uniq -c | sort -rn | xsel -b
-
HydraHatRack about 3 yearsIf you don't want a newline at the end of the copied content:
cat file | xargs echo -n | pbcopy
-
limido about 3 yearsno need for
cat
and pipelines :xclip file
-
Anton Krug about 3 yearsThat looks convoluted, what are the benefits compared to existing answers which use xsel?
-
awdr about 3 yearsyou dont have to install anything
-
Anton Krug about 3 yearsI would say that a single standalone xsel binary is nothing compared to the huge python3 framework. Some people might have xsel already installed, while not having python3. On top of that, your python script depends on the tkinter, which might not be installed as well. If the motivation of this answer to provide something with fewer dependencies, then I think it's exact opposite.
-
awdr about 3 yearsOn most linux systems is python already installed and tkinter is also includet.
-
galactica about 3 yearsI also found this is more natural on OS X, the up-voted one:
pbcopy < command
didn't work for me. -
Sebastián Vásquez about 3 years@alper, In WSL, instead of xclip use clip.exe. Check this: stackoverflow.com/a/44913872/3457432
-
alper about 3 years@SebastiánVásquez I am not using Windows I cannot run
clip.exe
-
alper almost 3 years
cd
v`` sayszsh: command not found: v
-
Ivan Gonzalez over 2 yearsThis should be a ZSH plugin!
-
kay over 2 years
pbcopy < [some alias command]
doesn't work. But[alias command] | pbcopy
does work. Thanks @HydraHatRack for newline fix. That helped my need. -
patrick.holzer over 2 yearsOne solution to rule 'em all! Big thanks, worked for me on MacOS - i'll test it on Linux too
-
DraganescuValentin about 2 yearspay attention to pbcopy to not confuse
<
with>
because with this>
you will paste into the file, possibly erasing it's contents -
Pablo Adames about 2 yearsIt hangs while doing the chaining example in Ubuntu 18.04. The tests show the same behaviour, tried running
cb --test
and it hangs on the changing test. -
Bruno Bronosky about 2 yearsThanks for the feedback @PabloAdames! I got some comments on the gist about it having when run over ssh. Are you running it on a local Ubuntu 18.04 or over ssh?
-
Fathy almost 2 yearsI created a tool here github.com/Ahmed7fathi/copycat
-
Pablo Adames almost 2 years@bruno-bronosky I found the reported hanging in Ubuntu 18.04 as the local operating system