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 Catch
However, what happens when we want to use the clipboard from a script?
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
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 -selection 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:
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:
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.