Map script to shortcut including examples


For Linux distributions it's very easy to assign a script to a keyboard shortcut, which can be very helpful in several cases. This tutorial should explain how to create a new bash script and assign this script to a new shortcut. For the explanation a Debian operating system with the desktop environment Mate was used. This tutorial also includes two very valuable bash scripts, to search or translate the currently selected word with your favorite browser.

First of all it's required to create a new bash script, like the sample scripts in this tutorial. The first script provides the feature to search the currently selected word or sentence. This is very useful to easily search for emerging error messages in the terminal or if an unknown word is displayed to you. The script requires the Linux tools xsel and xdotool. So initially it's required to install these tools via the local package manager, like the following command for the Debian operating system:

sudo apt-get install xsel xdotool

The script to search for the selected part currently uses the browser chromium with the search engine https://startpage.com/. All these options can be modified according to your needs. The line number five, which is currently added as comment, allows to read the information from the clipboard instead of the selected part. If this better fits your needs, it's possible to append just the option --clipboard to the existing xsel command.

1
2
3
4
5
6
7
#!/bin/bash
# read selected text
CLIPBOARD=$(/usr/bin/xsel)
# read from clipboard
# CLIPBOARD=$(/usr/bin/xsel --clipboard)
/usr/bin/xdotool search --name "Chromium" windowactivate
/usr/bin/chromium https://startpage.com/do/search?query="${CLIPBOARD/\&/%26}"

The same procedure can also be used to translate a selected word or sentence. To perform this, some minor changes of the previous described script are required. Currently the dictionary https://dict.leo.org is in use.

1
2
3
4
#!/bin/bash
CLIPBOARD=$(/usr/bin/xsel)
/usr/bin/xdotool search --name "Chromium" windowactivate
/usr/bin/chromium https://dict.leo.org/#/search="${CLIPBOARD/\&/%26}" --window --maximize

Now the scripts are prepared and the mapping of a script to a shortcut can be performed. The next step is to set the right permission to allow execution of the script. This can be done with the command chmod. The line below defines the script as executable. If you skip this step, you will get an error while executing the shortcut.

chmod u+x /path/to/script

To define a new shortcut in the Mate desktop environment, it's required to perform the following steps. Select System --> Hardware --> Keyboard Shortcuts. The location of the shortcut configuration can differ according to the Mate version. With the button Add you can append a new shortcut:

Add shortcut dialog

In this dialog you can specify a name for the shortcut, which maybe explains the function of the shortcut a little bit. In the command field it's required to insert the absolute path to the script, you want to execute. After you successfully specified the right options you can select the button Apply, which should insert a new line at the bottom of the shortcut list, with your defined name. If you did everything right you should now be able to use the specified shortcut to search for a selected word, like the example screenshots below show.

Proof of working shortcut

If now the shortcut is hit, the selected sentence Just a test will be searched with the defined search engine in the defined browser.

Proof of working shortcut

The bash scripts are also available on my github account: https://github.com/pycycle/bash-scripts