Emmabuntus, Ubuntu, Derivate, Linux, Open Source BackTrack, Linux, distributions, Ubuntu, derivate, securuty, forensic VirtualBox, Linux, Ubuntu, Raring Ringtail synaptic, Ubuntu, Linux, software packages jwplayer, multimedia, Linux, Ubuntu, flash Meshlab, graphic, software, Ubuntu, open source, Linux Synapse, Linux, Ubuntu, raring, Quantal Gimp, Ubuntu, Linux FreeMind, Linux, open source Linux, infographic, history

Xsel, access X selection from command line.

Xsel is a quick hack to give access to the X selection from the command line.

You can paste stuff from the X selection to stdout and copy stuff from
stdin to X selection. A few examples:

xsel -p | less            <i># pastes the current X selection to less</i>
xsel --paste | less <i># same as above</i>

echo puppa | xsel -c <i># 'puppa' is now the current X selection</i>
echo puppa | xsel --copy <i># same as above</i>
xsel -c "puppa" <i># same as above</i>

echo puppa | xsel -m <i># 'puppa' merged to the current X selection</i>
echo puppa | xsel --merge <i># same as above</i>
xsel -m "puppa" <i># same as above</i>

The above examples should also give you complete understanding of the xsel
syntax ;).

Further options:

xsel -c -display host3:0.0 "puppa"   #<i>Use X server on host3:0.0</i><br />xsel -c -s CLIPBOARD "puppa"         #<i>Use CLIPBOARD selection rather than PRIMARY</i><br />                                     #<i>You may use PRIMARY, SECONDARY or</i><br />                                     #<i>CLIPBOARD - also abbreviations</i><br />                                     #<i>P,p,S,s,C and c are valid.</i><br />xsel -c --selection CLIPBOARD "puppa" #<i>Same as -s.</i><br />

Note that the X selection model has no concept of selection buffer on the
server. Rather, the client that holds the current selection makes it
available to the other clients and has to wait until someone requests
it. Hence, xsel forks and serves the selection requests until someone else
claims the selection.

xsel.c is completely based on the Xfree86 xcutsel.c. Its author, Ralph Swick,
deserves more credit than I do.

What is it good for?

I originally began implementing this to able to select a word in X (in any
application) and get the Finnish-English/English-Finnish translation for it.
I've put the following hacks in the Enlightenment shortcuts configuration:

  • winkey-f1: dict.pl `xsel -p` | gmessage -nearmouse -geometry 440x300 -file - &

    (dict.pl is a perl script that fetches the
    translation from a web service (now that Mofile
    no longer provides this service, I use Kielikone's Netmot that is a commercial (non-free) service.))
  • winkey-f2: netscape-communicator -remote "openURL(`xsel -p`, new-window)"&

    Select an URL and open a netscape window for it with one key press.
  • winkey-f3: netscape-communicator -remote "openURL(http://www.google.com/search?q=`xsel -p|perl -pe 'tr/ /+/'`, new-window)"&

    Select a word and execute a Google search for it.

and so on. (I modified gmessage to exit on esc key press, and make selecting
text possible. The diff is available below. You can use standard xmessage as

You could also do something like

locate file | xsel -c

to get a large file listing into any application via X selection.


To compile:
gcc xsel.c -O2 -o xsel -lX11 -lXt -lXaw -lXmu -L/usr/X11R6/lib/

gcc `gtk-config --cflags` -O6 gmessage.c -o gmessage `gtk-config --libs`

(Too lazy to hack up a Makefile).

  • 0.01 13082000 vherva: Initial version
  • 0.02 14082000 vherva: Fixed -display command line switch. Fixed a hang
    with -p and empty selection. Some clean-ups.
  • 0.03 11062001 vherva: use XSetWMHints to start the program in
    iconic mode. This workarounds (or fixes,
    depending on how do you look at it) the
    orphaned window behaviour some folks were
    seeing. (You do get an entry to the KDE task
    bar etc, but that can be seen as useful, hence
    I'm lazy fix it.)
  • 0.04 21072002 vherva: Add --merge, --selection and clean up.
  • 0.04.1 30112002 vherva: Fix a typo in WriteStdout()

Adserver          610x250

If you liked this article, subscribe to the feed by clicking the image below to keep informed about new contents of the blog:


Share on Google Plus

About Hugo Repetto

Ubuntu is a Linux distribution that offers an operating system predominantly focused on desktop computers but also provides support for servers. Based on Debian GNU / Linux, Ubuntu focuses on ease of use, freedom in usage restriction, regular releases (every 6 months) and ease of installation.
    Blogger Comment
    Facebook Comment


Post a Comment