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):
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