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.txtThe 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.txtThe 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 -iThe -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.
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 -iFollowing 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.
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.
Figure AOne of the first drastic improvements I noticed (one that Unity should take serious advantage of when they finally migrate to the GNOME 3 libraries) is the launcher and pager. When the mouse moves up to the upper left-hand corner (or hitting Alt-F1), the launcher and pager appear (see Figure A). What’s immediately noticable is that the pager has undergone some serious reworking. Prior to this release, the pager simply displayed however many desktops were configured when the launcher was opened. Now the pager shows up mirroring the launcher where individual desktops can be selected. This image shows the pager expanded. By default, only the left edge of the pager will show. When you hover the mouse over that edge, the full pager will pull out and the specific desktop can be chosen.
Take, for instance, the GNOME 3 search. Built into GNOME 3 is a GNOME Do-like search/launch. You can call up the search window by opening the launcher (move the mouse to the upper left corner or hit Alt-F1), and then type a search string in the search field (see Figure B). If you want to search for something outside of your desktop, you can enter the search string and then click either Google or Wikipedia, and your default browser will open with the search results.
A writer for over 12 years, Jack's primary focus is on the Linux operating system and its effects on the open source and non-open source communities.
Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years.
Figure A After downloading, GNOME Do will install and be ready for use. But how do you use it? Tap the Super key, type gnome-do in the search area, and hit the Enter key to launch GNOME Do. Now, it gets a bit tricky. Under standard GNOME, the key combination to call forth GNOME Do is Super-Space, but the Super key is reserved for two very important functions in Unity. If Super is tapped once, it will call up the search dialog, and if it is pressed and held, the launcher icons will display numbers (see Figure A). The user can then press the number associated with the launcher to launch the application.
Figure BSo, after you launch GNOME Do, instead of using it to search for an application or file, the very first thing that must be done is to re-configure it to use a different hot key combination so that it doesn’t use the Super key. To do this, follow these steps: Launch GNOME Do with the gnome-do commandClick on the drop-down arrow in the upper-right corner and select PreferencesClick on the Keyboard tab in the Preferences windowDouble-click on the Summon Do Shortcut, and when it displays “New Accelerator,” enter the new shortcut to be used by pressing the key combination (I have configured it to use Ctrl-Space — See Figure B)Click close
Figure C When searching Google Calendar (or any of the Google plugins for GNOME Do), it will be necessary to previously authenticate with a Google account in the default browser or an error will occur. It is also possible, once authenticated against the Google Calendar account, to bring up the Google Calendar event add page with the help of GNOME Do. To do this: Open GNOME DoType “new event” (no quotes)Hit EnterWhen the Calendar icon appears in GNOME Do (see Figure C), hit Enter againThe default web browser will open to the Google Calendar Event Add page, which will allow you to add an event and save it