Copy the output of a command directly into the clipboard in Linux

2016-10-20

1  First install a 16 kB program called xclip:

sudo apt-get install xclip

2  You can then pipe the output into xclip to be copied into the clipboard:

cat file | xclip   (replace "cat file" with your own commands)

3  To paste the text you just copied, you shall use:

xclip -o

4  To simplify life, you can set up an alias in your .bashrc file as I did:

alias "c=xclip"
alias "v=xclip -o"


5  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 likeCtrl+Ton 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.


6 Only copy the content to the X clipboard

cat file | xclip


7 If you want to paste somewhere else other than a X application, try this one: (Much better!!!)

cat file | xclip -selection clipboard


8 Add this to to your ~/.bashrc:

# Now `cclip' copies and `clipp' pastes'
alias cclip='xclip -selection clipboard'
alias clipp='xclip -selection clipboard -o'

 (You can use whatever you want to replace cclip or clipp if you do not like them!)

You can add it  to your ~/.bashrc by running this:

printf "\nalias clipp=\'xclip -selection c -o\'\n" >> ~/.bashrc
printf "\nalias cclip=\'xclip -selection c -i\'\n" >> ~/.bashrc


Further reading:

http://stackoverflow.com/questions/5130968/how-can-i-copy-the-output-of-a-command-directly-into-my-clipboard5

本网站由阿里云提供云计算及安全服务