Copying Output of Commands in a Linux Terminal to X Selection or Clipboard

xclip is a tool to copy the copy the output of commands from a command line in a terminal to X selection or clipboard. It connects the command line interface and the X selections (clipboard) and is a very useful tool. For example, processing some text in vim in a terminal and then copying it to the browser will be easily handled by xclip.

There are lots good documents and tutorials on the Web including xclip man page, this tutorial and this one. I just give two very common use cases with the command examples. Make sure you have already installed it. On Fedora Linux, you can install it by # yum install xclip.

Copying a file (file.txt)’s content to the other X applications

2 methods here.

Method 1. Use X selection

First, copying the file content by

$ cat file.txt | xclip

Beside of using cat, you can also use xclip’s -i option.

Then, go the the X application’s input box and hit the middle key or scroll wheel.

Method 2. Use X clipboard

First, copying the file content by

$ cat file.txt | xclip -selection c

Then, go the the X application’s input box and press Ctrl + V.

Copying the clipboard content to a file (out.txt)

First, copying the file content in the X application by Ctrl + C as usually or by selecting them (using your mouse) as usual.

Then, saving the content you copied to a file.

$ xclip -o >out.txt

This step is the same no matter you use Ctrl + C or **selection in the previous step.

For more options, check the xclip man page.

About Eric Zhiqiang Ma

Eric Zhiqiang Ma is a PhD candidate at Dep. of CSE, HKUST. He is interested in system software for cloud computing, virtualization of large-scale distributed systems and etc. Also find Eric on Facebook, Twitter, LinkedIn and Google+. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties. More Posts - Website

Leave a Reply

Your email address will not be published. Required fields are marked *

six + 2 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Click to subscribe to Fclose.com Posts by Email if you have not.