xclip: Manipulating the clipboard from the command line

El clipboard o clipboard is a tool that the X server of our operating system offers us in order to share information between applications.

He is responsible for recurring operations such as cut, to copy y catch. It consists of a kind of warehouse or buffer where applications can store data so that these can be used later by the same or by other applications.

In general, we all know how to use it from a user point of view, either through application menus, or through keyboard shortcuts, usually:

  • Ctrl+X Cut
  • Ctrl+C Copy
  • Ctrl+V Take

However, what happens when we want to use the clipboard from a script?

xclip

xclip is an application that allows us to enter text on the clipboard and retrieve text from it from the command line. The retrieved text may have been entered by any other application.

Likewise, the text that is entered on the clipboard through xclip can be used by any other application.

The package xclip it can be found in the repositories of most distributions. For example, in Ubuntu you don't need more than an apt-get to install it:

$ sudo apt-get install xclip

Its basic use is very simple. To enter the standard input text on the clipboard, the option must be specified -i:

$ echo "Hola mundo" | xclip -i

The text "Hello world" will become available for the rest of the applications. Similarly, to retrieve the text from the clipboard and send it to standard output, the option -o:

$ xclip -o Hello world

National teams

The clipboard offers us three buffers or selections different:

  • PRIMARY: It is the default buffer. In it, text is stored simply by marking it with the cursor, without having to press a combination of keys or any option in the menu.
  • CLIPBOARD: This buffer is the most used by applications. It stores the text selected by the cursor only after you have pressed the key combination or menu option to cut or copy.
  • SECONDARY: It is an auxiliary and independent buffer. It is fully available but is rarely used by applications as standard. It is used only for very particular purposes.

xclip can manipulate all three buffers. It is only necessary to specify which one we want to use, through the option -selection and the first letter of the buffer name. By default, the buffer is used PRIMARY.

For example, to obtain the text that has been cut in another application, we must specify that we want the output content of the buffer CLIPBOARD, with options -o y -select c

$ xclip -o -selection c
Texto cortado en gedit

A practical application

The tool xclip offers many possibilities. It is mainly useful in scripts, where we do not have the possibility of using keyboard shortcuts, since the processes are carried out autonomously.

Let's take an example: let's imagine that we want to be able to search for any word that appears in any application or web page in Wordreference to translate it to English or from English with just one keystroke.

First, we must find out the URL format with which parameters are passed to the destination website. In this case it is the following:

http://www.wordreference.com/es/translation.asp?tranword=WORD

Once we know that, we simply have to create a script you open this url with for example Firefox, substituting WORD by the equivalent code that returns us the text found on the clipboard.

We will call it, for example, wordreference.sh, and it will contain the following:
#!/bin/bash
firefox http://www.wordreference.com/es/translation.asp?tranword=$(xclip -o)

Finally, once we have saved our script and we have given execution permissions, we have to associate it with a keyboard shortcut in our desktop manager. For example, we assign the combination Ctrl+G. And we have it ready.

Now we only have to use it. We mark with the cursor any word that we want to translate and press Ctrl+G. We will see how, without further ado, the browser opens with the page corresponding to the translation of the marked word.

It is a practical way to always have the translator at hand without installing dictionary applications.

Furthermore, this same method can be used to invoke any other web page, such as Google, Wikipedia or another site that allows us to perform searches, or to open applications that require parameters in your call.

More applications? What the imagination suggests.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   KZKG ^ Gaara said

    Good article, more like this are needed on the net.

    Since I made shortens [1] (mostly for my own benefit) I'm using xclip, mostly because it's multi-desktop, that is, it doesn't matter if it's Gnome or KDE or etc, I'll be able to manipulate clipboard data without problems 🙂

    regards

    [1] -» blog.desdelinux.net/tag/shortens/

    1.    beny_hm said

      admin a question how does your arch logo come out and I only get the fucking penguin XD I want that little T_T logo come out

  2.   wikimx said

    Excellent, xclip gives me an idea for a script with axel, if the script works out well I share it with you in this area 😀

  3.   let's use linux said

    Brilliant!

  4.   neysonv said

    Excellent article opens the doors to a lot of ideas. I only doubt remains; it's supposed to be for the x server, so the question is: will it work for wayland or mir ??? for xmir it is evident that yes but not in the other two cases.
    regards

  5.   poor taku said

    How cool, one more command for the arsenal