Hardware

Use xsel to copy text between CLI and GUI

The simple and versatile xsel utility bridges the gap between the Unix pipeline and the clipboard functionality of the X Window System.

The concept of a "clipboard" in common operating system usage is tied to graphical user interfaces (GUI), where a form of temporary storage makes it easier to duplicate or move text. Its use is pretty simple, though the details may vary between implementations. For instance, on MS Windows the usual way to copy text from a webpage to a text editor is to click and drag the mouse to highlight a block of text on the webpage and press Ctrl-C to copy, then click where you want to insert the text in your text editor and press Ctrl-V to paste. In the X Window System, meanwhile, a different approach is more common; click and drag the mouse to highlight the block of text in the browser that you want to copy, but do not click Ctrl-C, then use the mouse to point where you want to paste in the text editor and, without moving the mouse anywhere, click the middle mouse button.

In the world of the Unix command line interface (CLI), copying text around is generally much quicker and easier to automate. There is rarely any need to use the clipboard as a middle-man; simply use the Unix pipeline to stream text from one place to another. For instance, to copy the fifth paragraph from one text file and attach it to the end of another text file, one might use a command like the following:

head -n 5 foo.txt | tail -n 1 >> bar.txt

The head and tail commands can take the -n N option, where "N" is a number, to specify a number of lines of text that should be selected from either the beginning (head) or end (tail) of a file. By piping the output of head (in this case, the first five lines of text from a file) through tail (to select only the single last line of its input in this case), a specified chunk of text is essentially copied out of the middle of the file. The append redirect, >>, takes whatever is sent to it and attaches it to the end of the file whose name is specified to the right of the redirect.

For a more dramatic example, examine the case of copying the contents of two text files into a third, new file. To accomplish this in the graphical user interface, one would have to first open a text file, highlight its contents (using either Ctrl-A or a click-and-drag action with the mouse), and copy them, then open the new file and paste that text into it using Ctrl-V or a context menu. Next, open the second existing file, highlight, and copy -- then paste that into the new file after the text you had already pasted there. This involves either having multiple files open at once or opening and closing files an awful lot along the way, and takes some time on the user's part at every step.

To accomplish the same at the Unix shell, a simple command will suffice:

cat foo.txt bar.txt > baz.txt
The cat command was designed for the purpose of simply and easily concatenating two or more files' contents together. By default, that information is sent to standard output, but a truncating redirect, >, takes that output as its input and writes it to a file -- overwriting the contents of the file if it already exists, or creating the file from scratch if it does not already exist.

Ultimately, the process of copying and pasting is fairly simple to grasp whether using the Unix pipeline and CLI tools or using the standard, clipboard-based GUI approach of MS Windows and the X Window System. Given the computing environments in which most open source software users spend a lot of their time, both approaches are necessary parts of the toolsets at their disposal. There is one more case where yet another approach to copying text is needed, moving it between the CLI and the GUI.

Before continuing, a basic understanding of how clipboard-like functionality is handled in the X Window System is helpful. The X server supports an arbitrary number of selections, but the two most commonly used are the Primary Selection and Clipboard Selection. The Primary Selection is by default used to track currently selected text, while the Clipboard Selection is used as temporary storage when an application explicitly copies something to that selection.

The xsel tool, copyfree software available via the software management systems of most open source operating systems, is a simple utility meant to serve the need to copy text between the CLI environment and the GUI environment. It effectively acts as a pipeline-like interface between the CLI and the GUI clipboard. Anything that can be piped to a CLI utility or redirected to a file can be sent to the clipboard by way of an xsel command. For instance, to copy the contents of two files into the primary selection, this command suffices:

cat foo.txt bar.txt | xsel -i

The -i option, which simply directs xsel to read from standard input, is likely to be the most common way one would use the xsel utility, making it easy to load the contents of text files into the Primary Selection so they can be pasted into a GUI application with a middle click of the mouse. It acts like a truncating redirect, in that it replaces any current contents of the Primary Selection with whatever is piped to xsel -i. To cause xsel to behave more like an append redirect, use the -a option instead.

Those familiar with log monitoring with the tail utility will understand how xsel -f works. The manpage describes it thusly:

-f, --follow

append to selection as standard input grows. Implies -i.

At the other end of clipboard functionality is getting data out of one of the system's selections. To "paste" from the Primary Selection, actually writing the contents of it to standard output (thus allowing it to be sent through the Unix pipeline), you can use xsel -o. Thus, to write the contents of the Primary Selection to a new file:

xsel -o > foo.txt

This allows the data to flow in the other direction -- entered into the Primary Selection by selecting text in a GUI application and "pasting" into the Unix pipeline via the xsel utility.

The xsel utility also gives access to more than the Primary Selection. The Primary, Secondary, and Clipboard Selections can be accessed by use of the -p, -s, and -b options, respectively. The -p is generally unnecessary, though, because the Primary Selection is the default target of the xsel utility.

xsel offers other options, of varying levels of usefulness. The xsel tool as a whole, though, is of tremendous use for those who work extensively with both the command line and graphical user interfaces.

Practical example

This article was written using the Vim editor, and formatted using Markdown syntax. A command line filter utility I wrote in Ruby translates the Markdown formatted text of the article to HTML formatting; its output is piped to xsel. The command looks something like this:

muit filename.txt | xsel -i

Following that, I middle click to paste into the form used to submit the article for publication at TechRepublic. Every time I submit an article to TechRepublic (these days, generally fourteen times a month), I use xsel, in addition to my other uses of the tool.

About

Chad Perrin is an IT consultant, developer, and freelance professional writer. He holds both Microsoft and CompTIA certifications and is a graduate of two IT industry trade schools.

9 comments
apotheon
apotheon

The command options are slightly different; xclip is a much bigger program; xsel is copyfree licensed.

Sterling chip Camden
Sterling chip Camden

Having served on a few standards bodies, I can say that the raison d'etre for most standards is long history of bloody differences. Usually, the only thing the standard establishes is where the lines of incompatibility will be drawn. That's a useful function, but it usually doesn't solve the original problem.

apotheon
apotheon

All else being equal, it's a good thing to follow the standard. Where all else is not equal, it depends on a case-by-case analysis. JavaScript is one of those cases, because the ECMAScript standard just sucks.

seanferd
seanferd

It sounds like a good idea from the name, but I know not to trust any standard just because it calls itself a standard. Thanks for the enlightenment. (Bad. Bad thing.)

apotheon
apotheon

ICCCM is a horrifyingly bad standard. It's better that xsel supports it than not, though -- because it means greater tendency to be compatible with the behavior of other X software. As far as I'm aware, xclip is blissfully ignorant of ICCCM. I respect the high pain threshold of the xsel developer.

seanferd
seanferd

Xsel claims compliance with a standard of which I know little, but I have not seen such a reference in an xclip manpage. (Xsel conforms to the Inter-Client Communication Conventions Manual Version 2.0 (ICCCM2). ) Practically meaningless, or a good thing?

apotheon
apotheon

I get to pronounce it like (MS) Excel, but with the emphasis on the other wrong syllable.