Using xclip To Copy Output From The Command Line In Linux

When copying the standard output from within a terminal I often push it to a file using the "command > file" syntax. The trouble is that I then end up with a file that I have to open in order to get the output, and I often forget to delete the file once finished. This is especially annoying when I just want to paste some debug output into a help topic or similar. The solution to this is to use xclip to store the output in the xclipboard instead of a file. This is essentially the function of xclip, it allows access to a clipboard that you can store anything in.

A good example of this in use is when pasting the output of php -i into the amazing xdebug tailored instillation instructions. When you print this to the terminal you get pages of output. To push this into xclip instead just pipe it like this.

php -i | xclip

Xclip has access to two different areas where it can keep content, the default being the primary which is accessed when you middle click. To grab hold of the content in the above example you would middle click into a text document. Note that the primary is not the same as the one you access when you use the Ctrl-C, Ctrl-V controls, this is actually the secondary or clipboard area. To change the selection you can use the -selection parameter. Only the first character (or even just a few characters) of the selection are needed in order to tell xclip where to put the content. To tell xclip to use the clipboard you can use -selection parameter with the argument "secondary", "clipboard", "sec", "clip", "s" or "c", like this.

php -i | xclip -selection c

You can now paste the outputted content using Ctrl-C. There is plenty more to this little tool than I have gone into here, but what I have described above is perhaps the most common usage.

If xclip isn't installed on your system then you can easily install it using the following command.

sudo apt-get install xclip

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
14 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.