# 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            # pastes the current X selection to less
    xsel --paste | less       # same as above

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

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

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

Further options:

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

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 well.)

You could also do something like
`locate file | xsel -c`
to get a large file listing into any application via X selection.

## Download

- Source: xsel.c 0.04.1
- Man page: xsel.man 0.04.1
- Linux x86 redhat-6.2 binary for Xfree-4.0: xsel
- modified gmessage source: gmessage.c and the diff to gmessage-0.2
- Linux x86 redhat-6.2 gmessage binary

To compile: `gcc xsel.c -O2 -o xsel -lX11 -lXt -lXaw -L/usr/X11R6/lib/`
`gcc `gtk-config --cflags` -O6 gmessage.c -o gmessage `gtk-config --libs``
(Too lazy to hack up a Makefile).

## Versions

- 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() - thanks to José Fonseca <j_r_fonseca@yahoo.co.uk>

## Bugs

Xsel sometimes opens a small window that is not always visible. With my configuration, it never shows up, so I'm not eager enough to figure out how to get rid of it.

## Contact

v@iki.fi