
One of the reasons why I love KDE is the small scissor icon in the system tray, a sign that klipper is up and running for you.
The main task of klipper is to memorize the history of the clipboard and give an opportunity to navigate it Thus, even if you copied several pieces of information into the buffer by pressing
Ctrl + C , it is always possible to paste any of them by selecting it in the history and pressing
Ctrl + V.But you will find out what this modest-looking clipboard management program actually knows under the cat.
')
In addition to the standard functions, klipper allows you to extend its functionality using bash scripts, which means everything that we can do with the contents of the buffer is limited only by our imagination.
We will look at just a few of the most useful extensions. At once I will say that some of them (even in a more universal form) can be found on habrahabr, but firstly they look simpler in the klipper, and secondly I will show some new interesting techniques. Thus, we get a good selection of scripts for klipper.
A set of useful scripts for klipper
Editing the selected text in kate
For example, take a very simple task - the selected text needs to be edited in the advanced text editor kate.
To begin with, click on the klipper icon and enable actions with the clipboard by checking the corresponding box.
Then in the settings “klipper” on the “Actions” tab - disable all checkboxes and add a new action. In the properties of the action, we leave the field for the regular expression empty, since we want the action to be executed for any text; disable the “Automatically” checkbox so that klipper does not offer us to edit any text from the buffer in kate; and call it "Edit."
Next - add a command.

Let's call it "Edit with kate". The output of the command will be ignored, so we will select the appropriate item in the drop-down menu. And finally, we will enter the command itself, which will transfer the contents of the clipboard, denoted as
%s
, to the text editor kate
echo -n %s | kate -i -l 1 -c 1
Here
-n
used to suppress the output of the new timeline at the end of the text,
-i
to read data from the standard input,
-l 1
and
-c 1
to indicate the cursor position.
Thus, by copying arbitrary text into the buffer and pressing
Ctrl + Alt + R (the default combination can be changed to something more convenient), we will see the action menu klipper

After we select the item we need - Edit with kate, we will see a text editor window containing the selected text.
Link Actions
When meeting a link in the text, we often need to click on it or simply download the file that this link points to. To facilitate these actions, create a new action in klipper called Link. We will demand that this action be available only for links, for this we will write a regular expression for it (I didn’t bother much, I'm sure you will write better)
[htf]*p[s]*:\/\/.*
and disable the automatic execution of this action.
Next - add two commands. One - to follow the link, and the second - to download the file. In both cases we will ignore the output of the command and give them clear descriptions.
Open link in new tab in firefox
firefox -new-tab %s
Get the file with wget
wget -qcP ~/ %s
In the latter case, it is convenient to add an alert about the end of the download using kdialog
wget -qcP ~/ %s; kdialog --title "Download completed" --passivepopup "Downloaded file $(echo %s | grep -o "[^\/]*" | tail -n 1)" 2
Although in this case, check whether the file was loaded successfully will have to manually. A link to a more thoughtful and complete version of the script (which is nevertheless better not to be written in one line) is contained at the end of the article.
Fix the layout problem
"Ghbdtn! Rfr ltkf & "- these words are probably familiar to everyone, and if you can learn from the other hand the method of blind printing, put xneur or at least manage layouts at the window level, then there is no chance to protect yourself from others. Klipper will help us deal with this problem again.
Create an action under the name Transcode, suitable for any text and starting manually. Add to this action two teams - transcoding from English to Russian and vice versa. We give these commands a meaningful description and set the value of the "Command output" field to "Copy to clipboard." The teams themselves will be
echo -n %s | sed "y/qwerty//"
Sed will replace qwerty with Yutsuken here (links to full versions of commands are given at the end of the article).
Vocabulary
While writing a dissertation, I often have to translate my articles from English into Ukrainian and vice versa. And if the text as a whole is translated quite smoothly, then with individual words you sometimes have to suffer, looking for suitable synonyms. Klipper can help us in this difficult matter.
Create the scripts directory in the home folder and rewrite the dict_en_ru.sh file there (the link to it is given at the end of the article). In klipper create a new action, let's call it a Dictionary. Leave the regular expression field empty and remove the "Auto" checkbox. Then add a new command, call it Translate a word from English to Russian. The output of the command as in the previous example will be copied to the clipboard. The team itself will be nothing more than the launch of our script.
bash ~/scripts/dict_en_ru.sh %s
We carry out similar actions for the reverse direction of the translation (if you need the FROM_LANG and TO_LANG variables in the script, and change the translation languages)
Below is a description of the script
- Select a word or sentence, press Ctrl + Alt + R and select from the list of commands - Translate a word from English to Russian
- The script takes the first word from the selection.
- Makes a request to translate.google.com for translation
- Parses the answer and selects from it different values ​​of the translation for the desired word.
- Displays the result using kdialog as a list of possible translations.
- When you click on OK, the selected translation option is copied to the clipboard and can be inserted into the text using the Ctrl + V combination .
Here's what it looks like.

Well, that's all for today. Next time we will teach klipper new tricks and if you like it, we will continue to develop the theme of scripts in KDE.
Links to scripts
download.shtranscode_ru_en.shtranscode_en_en.shdict_ru_en.shdict_en_ru.shLook for beautiful knees, write scripts and stay with us!