📜 ⬆️ ⬇️

Working with the clipboard in Linux: theory and practice

Quite a bit of theory


Copy and Paste Historically, in the X Window System (X11, is a window system for Linux, UNIX), there are two clipboards.

One of them (clipboard) is similar to the clipboard in Windows - when you press Ctrl + Insert or Ctrl + C, the selected fragment (text, picture, file) is copied to the clipboard, and when you press Shift + Insert (or Ctrl + V) - is inserted from it. It should be noted that in many programs these combinations are reserved for other purposes and you have to use others - for example, in the terminal, the combination Ctrl + C is used to complete the process, and to work with the clipboard, the combinations Ctrl + Shift + C are used for copying and Ctrl + Shift + V to insert.

The second buffer (primary) is specific to the X11 window system. The selected text immediately goes into the primary buffer, and in order to paste the copied text, you just need to press the middle mouse button (wheel). Those who do not have a three-button mouse, as well as owners of laptops with touchpads should simultaneously press the left and right mouse buttons to insert text.
')
Usually these buffers are not related to each other (some programs incorrectly process them and consider that this is the same clipboard). Consequently, the data stored in them does not affect each other, which is undoubtedly extremely convenient. It should be noted that when you close the program from which the data was copied, the contents of the clipboard are lost.


Practice


To solve the problem of losing data from the clipboard when closing the program, there is third-party software. For example, Clipboard Daemon . This little daemon keeps the contents of the clipboard in memory, regardless of whether the application from which the data was copied was closed.

For a more comfortable work with the clipboard, there are a number of programs:These programs make it much easier to work - they keep the history of the contents of the clipboard - you can return to any of the previous states at any time (within reason, of course, this limit, as usual, is set in the settings) and use it =)

There is also a very, very useful in skilled hands utility called xclip , designed to work with clipboard from the command line. Copying and pasting text is carried out by simple commands, which allows it to be used in various kinds of auxiliary scripts, examples of which I will demonstrate below.

Unfortunately, the official version of xclip did not work properly with my Cyrillic alphabet, despite the fact that I collected the latest version. Therefore, I suggest downloading and compiling a version of xclip for the Alt Linux distribution .


Scripts


I offer two scripts that can facilitate your daily work. The first script:
xclip -o | sed -r '2~1d;s/(^\s+|\s+$)//g;s/%/%25/g;s/#/%23/g;s/\$/%24/g;s/&/%26/g;s/\+/%2B/;s/,/%2C/g;s/:/%3A/g;s/;/%3B/g;s/=/%3D/g;s/\?/%3F/g;s/@/%40/g;s/\s/+/g' | awk '{print "http://www.google.ru/search?hl=ru&q=" $1}' | xargs firefox -new-tab
It opens a tab in Firefox, goes to the google page with a search phrase that is the content of your clipboard. To work with a script, it is enough to select any word, phrase or sentence and run the script (I recommend assigning the execution of this script to hot keys - for example, I have this combination Win + G). Consider it a little more:
  1. The xclip program displays the contents of the clipboard (parameter "-o").
  2. Further, this content is transferred to the text editor sed, which deletes all lines except the first (if they were) and replaces all special characters with their safe for address line option (urlencode).
  3. The processed string is passed to the awk program, which, in turn, adds the resulting search phrase to the google link.
  4. The link opens in a new tab, Firefox. All =)
The second script is slightly simpler, and logically follows from the first:
xclip -o | sed -n 1p | xargs firefox -new-tab
It opens a new tab in Firefox with the address that is on the clipboard (very often you need to open the link as plain text - for example, if the link is found in a text editor - you have to copy it, open the tab in the browser and paste the copied address. The script does everything for you ;). I assigned it to a combination of Win + F.

Due to the fact that the clipboard is a universal entity for the OS, these scripts will work everywhere - from the terminal and text editor to Firefox itself (however, those who wish can customize the same script for alternative browsers. You can assign a combination of buttons to scripts as using your windows manager (for example, gconf-editor for Gnome), or using third-party programs like xmodmap or actkbd.


What's next?


Yes, anything =) You can translate phrases highlighted by the mouse, you can copy them into the program for notes - it all depends on your imagination and needs. Of course, for such things there can be separate programs, but such self-written scripts, in my opinion, will be most convenient for any user - linux is so good that everything, absolutely everything can be customized for you and for yourself.


Update : added a link to Parcellite clipboard manager - thanks to drujebober

Update 2 : at the request of dimaka habrauser added scripts for translation:

Lingvo.yandex.ru:
xclip -o | sed -r '2~1d;s/(^\s+|\s+$)//g;s/%/%25/g;s/#/%23/g;s/\$/%24/g;s/&/%26/g;s/\+/%2B/;s/,/%2C/g;s/:/%3A/g;s/;/%3B/g;s/=/%3D/g;s/\?/%3F/g;s/@/%40/g;s/\s/+/g' | awk '{print "lingvo.yandex.ru/en?st_translate=on&text=" $1}' | xargs firefox -new-tab

Google translate (translation from English to Russian):
xclip -o | sed -r '2~1d;s/(^\s+|\s+$)//g;s/%/%25/g;s/#/%23/g;s/\$/%24/g;s/&/%26/g;s/\+/%2B/;s/,/%2C/g;s/:/%3A/g;s/;/%3B/g;s/=/%3D/g;s/\?/%3F/g;s/@/%40/g;s/\s/+/g' | awk '{print "translate.google.com/translate_t?hl=ru#en|ru|" $1}' | xargs firefox -new-tab

Update 3 : Adding the item “copy the full path of the current file” in the Midnight Commander menu (Add to the file ~ / .mc / menu):
+ ! t t<br>f Copy full filename into clipboard<br>echo -n %d/%f | xclip


_________
The text was prepared in the editor VIM ;)

Source: https://habr.com/ru/post/48954/


All Articles