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
[ Parent | Reply to this comment ]
And you can see the date in clipman
[ Parent | Reply to this comment ]
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 ]
[ Parent | Reply to this comment ]
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 ]
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 ]
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 ]
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 ]
(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 ]
[ Parent | Reply to this comment ]
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 ]