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
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
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.
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
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.
_copy(){
cat | xclip -selection clipboard
}
_paste(){
xclip -selection clipboard -o
}
_copy(){
cat | pbcopy
}
_paste(){
pbpaste
}
_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."
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
$ date | cb
# clipboard contains: Tue Jan 24 23:00:00 EST 2017
# 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
$ 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
(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
Add this to to your ~/.bashrc
:
# Now `cclip' copies and `clipp' pastes'
alias cclip='xclip -selection clipboard'
alias clipp='xclip -selection clipboard -o'
Now clipp pastes and cclip copies — but you can also do fancier stuff:
clipp | sed 's/^/ /' | cclip
↑ indents your clipboard; good for sites without stack overflow's { } button
You can add it by running this:
printf "\nalias clipp=\'xclip -selection c -o\'\n" >> ~/.bashrc
printf "\nalias cclip=\'xclip -selection c -i\'\n" >> ~/.bashrc
I am using Parcellite and xsel
to copy last commit message from git
to my clipboard manager (for some reason xclip
does not work):
$ git log -1 --pretty=%B | xsel -i -b
Without using external tools, if you are connecting to the server view SSH, this is a relatively easy command:
From a Windows 7+ command prompt:
ssh user@server cat /etc/passwd | clip
This will put the content of the remote file to your local clipboard.
(The command requires running Pageant for the key, or it will ask you for a password.)
I made a small tool providing similar functionality, without using xclip or xsel. stdout
is copied to a clipboard and can be pasted again in the terminal. See:
https://sourceforge.net/projects/commandlinecopypaste/
Note, that this tool does not need an X-session. The clipboard can just be used within the terminal and has not to be pasted by Ctrl+V or middle-mouse-click into other X-windows.
In Linux with xclip installed:
xclip -selection clipboard < file
Based on previous posts, I ended up with the following light-weigh alias solution that can be added to .bashrc
:
if [ -n "$(type -P xclip)" ]
then
alias xclip='xclip -selection clipboard'
alias clipboard='if [ -p /dev/stdin ]; then xclip -in; fi; xclip -out'
fi
Examples:
# Copy
$ date | clipboard
Sat Dec 29 14:12:57 PST 2018
# Paste
$ date
Sat Dec 29 14:12:57 PST 2018
# Chain
$ date | clipboard | wc
1 6 29
For those using bash installed on their windows system (known as Windows Subsystem for Linux (WSL)), attempting xclip will give an error:
Error: Can't open display: (null)
Instead, recall that linux subsystem has access to windows executables. It's possible to use clip.exe like
echo hello | clip.exe
which allows you to use the paste command (ctrl-v).