Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 behaviour: 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.

Any help would be appreciated!

Thanks

share|improve this question
add comment

2 Answers 2

up vote 11 down vote accepted

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

share|improve this answer
add comment

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

share|improve this answer
    
You can edit your answer to add hyperlinks to the manpages. (You should be able to add up to 2 links, even though you're a new AU user.) –  Eliah Kagan Jul 7 '12 at 13:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.