Command Line Clipboard Access

I'm building a command line tool in order to make some of my tasks more efficient. I need to be able to write content to the clipboard, but for some reason it's not working for me.

I've tried installing both xclip and xsel, but both have the same behavior: they can read/write to the clipboard fine, but I cannot access it from the main Ubuntu GUI.

I've also tried it in the terminal...

echo "hello" | xclip

Then if Ctrl+Shift+V, I get my original clipboard entry, not "hello". But, if I try to grab the content from xclip, it works fine.

The main goal is get the content pastable into Firefox or whatever other GUI-based programs I'm using.

3 Answers

Well, there are a few different clipboards in X :) The one xclip copies text into by default is "selection buffer" - usually you just select some text with your mouse and then can paste it with middle mouse button. This buffer is separate from the one from which you can paste with Ctrl-Shift-V.

Try

echo "hello" | xclip -selection clipboard

also, see

man xclip

for more details about xclip

3

To make it easier for myself I created an alias for xclip in order to mimic the functionality of pbcopy and and pbpaste in Mac OS X.

sudo apt-get install xclip -y

Then edit your ~/.bashrc to add aliases:

nano ~/.bashrc

Add these new lines (you could put them at the end of the file):

alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'

Save and exit, then open a new shell or run source ~/.bashrc to use the aliases.

My blog post contains further details.

0

I used to use the utilities wxcopy and wxpaste from windowmaker, but recent Linux versions (or X versions) seem to have broken them - I suspect security has been tightened up and they have not been updated to match. E.g. you could do things like:

echo fred | wxcopy | tr "a-z" "A-Z" | wxpaste

to get the output FRED. (It's a contrived example, since you'd get the same thing without the final wxpaste, but I think it gives the flavour of what you can achieve.)

However, you can achieve the same effect using the "xcb" package, which is incredibly lightweight and also provides a tiny (summarised) visual display of 8 clipboards.

I wrote a pair of shell scripts wcopy/wpaste years ago, to make wxcopy/wxpaste a bit more pleasant to my taste. I updated them tonight to work with either wxcopy/wxpaste or xcb. That makes them a bit more complex than they need to be, but I'll paste them in here - hopefully they're not too long for this forum.

Here's wcopy:

#!/bin/sh
#
# Provide some enhancements to the wxcopy command, which copies standard input
# to an X11 clipboard text buffer.
#
# Allow copying from stdin to any of the cutbuffers. Note that they are
# indexed counting from 0.
#
# Author: Luke Kendall
#
if [ `uname -s` = "Darwin" ]
then WXCOPY=pbcopy WXPASTE=pbpaste
else WXCOPY=wxcopy WXPASTE=wxpaste BUFSPEC="-cutbuffer" xcb -p 0 > /tmp/wc$$ if echo "fred$$" | wxcopy -cutbuffer 0 && [ `wxpaste` = "fred$$" ] then : # Great, they're actually working. Not common on modern Linuxes. echo "working" > $HOME/.wcopyok else rm -f $HOME/.wcopyok WXCOPY="xcb -s" WXPASTE="xcb -p" BUFSPEC= fi xcb -s 0 < /tmp/wc$$
fi
unset WXARGS
if [ $# = 0 ]
then $WXCOPY ${WXCOPY_DEFS:-0}
else MYNAME=`basename $0` USAGE="usage: $MYNAME [ [0-9]... ] [$WXCOPY's args]" numlist=true for n do if $numlist && expr "x$n" : 'x[0-9][0-9]*$' > /dev/null then NUMARGS="$NUMARGS $n" else numlist=false if [ "x$n" = "x-h" ] then echo "$USAGE" >&2 exit 0 else WXARGS="$WXARGS $n" fi fi done set - $NUMARGS $WXCOPY $WXCOPY_DEFS $WXARGS $BUFSPEC $1 ORIG="$1" shift for n do $WXPASTE $BUFSPEC $ORIG | $WXCOPY $WXCOPY_DEFS $WXARGS $BUFSPEC $n done
fi

And here's wpaste:

#!/bin/sh
#
# Provide some enhancements to the wxpaste command, which pastes from X11
# clipboard text buffers to standard output.
#
# Allow pasting to stdout from any of the cutbuffers. Note that they are
# indexed counting from 0.
#
# Author: Luke Kendall
#
if [ `uname -s` = "Darwin" ]
then WXCOPY=pbcopy WXPASTE=pbpaste
else WXCOPY=wxcopy WXPASTE=wxpaste BUFSPEC="-cutbuffer" if [ -s $HOME/.wcopyok ] then : # Great, they're actually working. Not common on modern Linuxes. else WXCOPY="xcb -s" WXPASTE="xcb -p" BUFSPEC= fi
fi
if [ $# = 0 ]
then $WXPASTE ${WXPASTE_DEFS:-0}
else MYNAME=`basename $0` USAGE="usage: $MYNAME [ [0-9]... ] [$WXPASTE's args]" for n do if expr "x$n" : 'x[0-9][0-9]*$' > /dev/null then NUMARGS="$NUMARGS $n" elif [ "x$n" = "x-h" ] then echo "$USAGE" >&2 exit 0 else WXARGS="$WXARGS $n" fi done set - $NUMARGS : echo "Num args: $#" for n do : echo "Doing: $WXPASTE $WXPASTE_DEFS $WXARGS $BUFSPEC $n" $WXPASTE $WXPASTE_DEFS $WXARGS $BUFSPEC $n done
fi

If anyone's interested, I wrote man pages for the scripts, too - but you can probably find them (they're still valid) by googling wcopy.1x and wpaste.1x

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like