How to Copy 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.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

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