Command-Line Copy&Paste With xclip (Debian/Ubuntu)

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Thu, 2009-10-08 11:16. :: Debian | Ubuntu

Command-Line Copy&Paste With xclip (Debian/Ubuntu)

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Follow me on Twitter
Last edited 08/26/2009

xclip is a command line interface to the X11 clipboard. It allows you to put the output of a command directly into the clipboard so that you don't have to copy&paste from the terminal manually (which can be a tedious task especially if the output is very long). It also allows you to put the contents of a file directly into the clipboard.

I do not issue any guarantee that this will work for you!

 

1 Installing xclip

xclip is available as a package for Debian and Ubuntu so that it can be installed with aptitude. Open a terminal (Applications > Accessories > Terminal)...

... and run:

sudo aptitude install xclip

 

2 Using xclip

To put the output of a command into the clipboard, we have to pipe the command into xclip, e.g. like this:

ls -la | xclip

This puts the output of the ls -la command into the clipboard, and you can now paste the output into any other program (e.g. a text editor) by pressing the middle button of your mouse:

To put the whole contents of a file (e.g. /etc/apt/sources.list) into the clipboard, you'd use xclip as follows:

xclip /etc/apt/sources.list

You can use xclip as well to output the contents of the clipboard:

xclip -o

And to save the contents of the clipboard to a file (e.g. ~/test.txt), you'd call xclip as follows:

xclip -o > ~/test.txt

To learn more about xclip, take a look at its man page:

man xclip

 

3 Links


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Juan Carlos Perez (not registered) on Tue, 2010-01-26 02:19.
It would be very useful for collecting clips of text in a file. But results are poor because of its lack of support of unicode.
Submitted by Anonymous (not registered) on Thu, 2009-10-15 08:00.

For the first example, why not just save the output of a command to a temporary file? And then load that file with the editor?

ls -la > /tmp/dirlisting

gedit /tmp/dirlisting

Or even skip the temp file, and let gedit load it directly (Yes you can launch gedit from the CLI, and yes, it can load content from its standard input - http://www.digipedia.pl/man/view/doc/gedit.1.html )

ls -la  | gedit

And if you want to load it into some text from a file into a gedit session you have open already with other text, I'm sure somewhere under 'file' or 'edit' there is 'insert file'

For the second, why put a file into some magic place called a 'clipboard', just to paste it into something else? Why not just load it directly from the file into the 'something else'?

Really, copy and paste seems to be ill-suited for anything larger than a paragraph. And if its sufficiently inconvenient to select it with the mouse due to its size, having some special program load it into the magic 'clipboard' seems a rather confused approach. But thats whats wonderfing about unix/linux, there are so many ways to do the same thing, and even terribly confused people can come up with new ways, if it somehow makes sense to them.
Submitted by Anonymous (not registered) on Sun, 2009-11-15 15:39.

o " ls -la  | gedit "  - doesn't work

o  gedit http://www.gnome.org - sweet

o " having some special program load it into the magic 'clipboard' seems a rather confused approach. But thats whats wonderfing about unix/linux, there are so many ways to do the same thing, and even terribly confused people can come up with new ways, if it somehow makes sense to them."    - snarky much ?