Using the X clipboard from the command line

Posted by JoshTriplett on Mon 26 Nov 2007 at 09:07

Ever want to copy the output of a command to the clipboard and paste it elsewhere? How about pasting into the input of a command? xclip lets you do exactly that.

xclip uses the X Window System libraries, but it doesn't pop up a graphical interface. Instead, it either copies its standard input to the X clipboard, or copies the X clipboard to its standard output.

xclip defaults to the more common behavior: copying standard input to the clipboard. Simply pipe output into xclip, and it will end up in the X primary clipboard, where you can paste it with a middle-click. For instance, you might want to paste the output of lspci -n into a web form to tell you what drivers you need; run lspci -n | xclip and then middle-click in the form.

To copy the X clipboard to standard output, run xclip -o. For instance, you might have just copied an interesting script from this site, and want to paste it into a file to run it; run xclip -o > handy-script, and you now have the script in a file, ready to tweak, chmod, and run.

As with other uses of copy and paste in X, you need to remember that X has two main clipboards (as well as others less commonly used):

PRIMARY
Normally copied to implicitly with mouse selection, and pasted with a middle mouse click.
CLIPBOARD
Normally used explicitly from applications, such as with C-c, C-x, and C-v.

xclip defaults to the X PRIMARY. If you want it to use the X CLIPBOARD instead, use xclip -selection c to copy standard input to it, or xclip -o -selection c to output it.

Finally, a useful snippet for your .bashrc, which uses xclip and various readline commands to make Ctrl-v paste the X clipboard on the bash command line:
# Make Control-v paste, if in X and if xclip available - Josh Triplett
if [ -n "$DISPLAY" ] && [ -x /usr/bin/xclip ] ; then
    # Work around a bash bug: \C-@ does not work in a key binding
    bind '"\C-x\C-m": set-mark'
    # The '#' characters ensure that kill commands have text to work on; if
    # not, this binding would malfunction at the start or end of a line.
    bind 'Control-v: "#\C-b\C-k#\C-x\C-?\"$(xclip -o -selection c)\"\e\C-e\C-x\C-m\C-a\C-y\C-?\C-e\C-y\ey\C-x\C-x\C-d"'
fi

 

 


Posted by Anonymous (213.120.xx.xx) on Mon 26 Nov 2007 at 11:50
Great tip, I didn't know about xclip before. However, the host system would have to have X11 installed.

Personally, I use the Xfce utility Clipman constantly. Clipman can capture from both xterms and GUI applications, store clipboard history and configure how big the history should be. I have just tested using xclip and Clipman together - pipe several commands into xclip and then pick them up later using Clipman - cool!

[ Parent | Reply to this comment ]

Posted by Anonymous (87.171.xx.xx) on Mon 26 Nov 2007 at 15:53
But you can't use clipman from command line?

[ Parent | Reply to this comment ]

Posted by Anonymous (78.246.xx.xx) on Mon 20 Aug 2012 at 01:32
date | xclip -selection clipboard
And you can see the date in clipman

[ Parent | Reply to this comment ]

Posted by JoshTriplett (66.93.xx.xx) on Tue 27 Nov 2007 at 00:10
However, the host system would have to have X11 installed.

Just the X client libraries, not necessarily a full installation of X, and in particular not necessarily an X server.

For that matter, by the way: note that this will work fine over an X connection forwarded via ssh -X.

[ Parent | Reply to this comment ]

Posted by Alucard (130.215.xx.xx) on Mon 26 Nov 2007 at 15:31
[ View Weblogs ]
Hmm.. can I combine X PRIMARY and X CLIPBOARD somehow? ie, highlighting and ^C copy to the same/both, and middle-click and ^V paste the same thing?

[ Parent | Reply to this comment ]

Posted by JoshTriplett (66.93.xx.xx) on Tue 27 Nov 2007 at 00:07
You theoretically could, but then you lose some functionality. Say you want to copy some text and paste it over some other text. Select the first text, copy it with Ctrl-C, select the second text...oops, you just lost your first copy.

That said, one particular behavior I've observed in several applications makes a lot of sense to me: when copying to PRIMARY, only copy to PRIMARY, but when copying to CLIPBOARD, also copy to PRIMARY. Firefox^WIceweasel does this, for instance.

[ Parent | Reply to this comment ]

Posted by mverwijs (195.169.xx.xx) on Mon 26 Nov 2007 at 16:18
Alternative: xsel

Does the same stuff, but (if I diff the manpages of both programmes) seems to do a bit more.

Kind regards,

-mverwijs

[ Parent | Reply to this comment ]

Posted by lpenz (201.21.xx.xx) on Wed 28 Nov 2007 at 19:23
I have two python scripts, one syncs primary and clipboard, as rxvt only knows primary and firefox likes clipboard better.
The other appends the contents of clipboard whenever it changes. By using root-tail, I am able to always see what I have in the clipboard in the X background. Very useful.

[ Parent | Reply to this comment ]

Posted by wulf (85.178.xx.xx) on Thu 3 Jan 2008 at 01:16

yes, with a bit surroundings it's very convenient. I use a small script in combination with xsel and windowmanager key binding too figure around daily hassle with small input fields. Also it brings all the lovely features of your favorite editor while you work on wiki pages or in contact-mail forms on the web. I will never miss something like that.

http://gpl.coulmann.de/xseledit.html

Maybe someone else think it's usefull

Best wishes
Wulf

[ Parent | Reply to this comment ]

Posted by Anonymous (76.199.xx.xx) on Sun 2 Dec 2007 at 23:40
A while ago, I wrote about how to make "cut", "copy", and "paste" work the same way on the command line as it works in the shell. I used xclip of course.

(ignore the awful theme; I'll change it soon...)

http://news.u32.net/articles/2006/07/31/shell-vs-file-manager

[ Parent | Reply to this comment ]

Posted by Anonymous (200.138.xx.xx) on Fri 18 Jan 2008 at 13:02
Xclip was not able to handle accented characters properly, what is probably due to some enconding issues. Xsel, on the other hand, worked like a charm.

[ Parent | Reply to this comment ]

Sign In

Username:

Password:

[Register|Advanced]

 

Flattr

 

Current Poll

What do you use for configuration management?








( 692 votes ~ 10 comments )