Developers Club geek daily blog

2 years, 2 months ago
We configure the keyboard shortcuts in Linux like Mac OS X
Earlier, I quite often had such situation when at the same time you work in the terminal and, for example, in the browser.
After several business hours you start being confused and in the terminal instead of [Ctrl] + [Shift] + [C] you click [Ctrl] + [C], and in the browser on the contrary. As a result in the terminal you receive interruption and in the browser instead of the expected effect at you the debagger slowly progruzhatsya.
Once it has got me and I have decided that it is time to change something...

Before I have reinstalled by the working machine OSX on Linux, I have managed to get used to quite pleasant implementation of the keyboard shortcuts. To be exact to that all main operations, such how "to Cut", "Copy" and "Insert", all of them use the button [Cmd] (analog of the button [Win] on PC), but not [Ctrl] as it is made by default in Linux and Windows. It is very convenient as, I will repeat if you often work in the terminal, you have the uniform keyboard shortcuts for copying and insert as well as in all other applications, and interruption always remains on the place [Ctrl] + to [C].
You also oluchit profit from such way if you use the taylovy window manager since in most cases for use of standard operations and switchings between tags/windows you should not remove finger from your modkey

Of course it is possible to configure the keyboard shortcuts in configs of each certain application, but it too is not convenient, besides not each application supports such setup. On it I have decided to zabindit simply the standard keyboard shortcuts that are necessary to me.
I.e. clicking [Win] + [C], your terminal will think that you click [Ctrl] + [Shift] + [C], and all other applications that [Ctrl] + [C].

xbindkeys will be necessary for implementation conceived to us the program which will trace our clicking, for example or it is possible to use regular opportunities of your window manager, and the emulator of clicking keys, them a little: xdotool, xte and xvkbd.

The truth with the first two I had one most very interesting glitch: if for example you nazhimt [Win] + [A], the emulator will obediently transfer [Ctrl] + to [A] to application, and application will see that all three keys [Win] + by [Ctrl] + are clicked by [A] and will not process such combination. This very unpleasant bug was not simple to be found as the option - clearmodifiers for xdotool which, apparently, especially for such case and is created did not help even. In xkdbb of similar behavior it has not been noticed.

Let's begin, perhaps from xbindkeys. Otklyuchem all CapsLock, NumLock and other modifiers, we start:
$ xbindkeys -k

The window opens, in window we click combination [Win] + [A], in the terminal we receive such output:
"(Scheme function)"
    m:0x40 + c:38
    Mod4 + a

where Mod4 — is our key [Win]. Same it is possible to use for writing of config of xbindkeys:

We open ~ / .xbindkeysrc and we write:
"xvkbd -xsendevent -text '\[Control_L]a'"
    Mod4 + a
Option for xdotool and xte
xdotool
"xdotool key --delay 0 --clearmodifiers ctrl+a"
    Mod4 + a

xte
"xte 'keydown Control_L' 'key A' 'keyup Control_L'"
    Mod4 + a



Now we will start xbindkeys:
$ xbindkeys

Also we will try to click [Win] + [A], the effect will be same as well as if you have clicked [Ctrl] + [A]

After that that you are convinced that everything works, it is possible to continue to govern kofig, but before next run it is necessary with has started completing the previous process of xbindkeys:
$ pkill xbindkeys


Ok, have dealt with it but how to transfer us the different keyboard shortcuts, when clicking the same buttons, depending on the program with which we work?
The same xdotool and xprop comes to the rescue, whether by means of which we define is the active window the terminal.

Let's write unpretentious script and we will put it in / bin/copypaste.sh:
#!/bin/bash
W=`xdotool getactivewindow`
S1=`xprop -id ${W} |awk '/WM_CLASS/{print $4}'`
S2='"URxvt"'                                               #Мой любимый терминал
S3='"XTerm"'                                               #Мой второй терминал

if [ $1 = "copy" ]; then                                   # Проверяем аргумент на copy                                    
    if [ $S1 = $S2 ] || [ $S1 = $S3 ]; then                # Если это терминал
    xvkbd -xsendevent -text '\[Control_L]\[Shift_L]\[C]'   # Отправляем [Ctrl]+[Shift]+[C]
    else                                                   # Если нет 
    xvkbd -xsendevent -text '\[Control_L]c'                # Отправляем [Ctrl]+[C]
    fi  
fi

if [ $1 = "paste" ]; then                                  # Тоже самое для аргумента paste
    if [ $S1 = $S2 ] || [ $S1 = $S3 ]; then
    xvkbd -xsendevent -text '\[Control_L]\[Shift_L]\[V]'
    else
    xvkbd -xsendevent -text '\[Control_L]v'
    fi  
fi

Use xprop if you do not know what class your program uses:
xprop
and click at window

We do not forget to make script executed:
# chmod +x /bin/copypaste.sh

It works here so:
$ copypaste.sh copy
$ copypaste.sh paste

Same also we will add in our config ~ / .xbindkeysrc:
"xvkbd -xsendevent -text '\[Control_L]a'"
    Mod4 + a
"copypaste.sh copy"
    Mod4 + c
"copypaste.sh paste"
    Mod4 + v


In this way we configure combinations for other keys.

Ready config:
for xbindkeys
~ / .xbindkeysrc
"copypaste.sh copy"
    Mod4 + c 
"copypaste.sh paste"
    Mod4 + v 
"/usr/bin/xvkbd -xsendevent -text '\[Control_L]x'"
    Mod4 + x 
"/usr/bin/xvkbd -xsendevent -text '\[Control_L]z'"
    Mod4 + z 
"xvkbd -xsendevent -text '\\[Control_L]\\[Shift_L]\\[Z]'"
    Shift+Mod4 + z 
"/usr/bin/xvkbd -xsendevent -text '\[Control_L]q'"
    Mod4 + q 
"/usr/bin/xvkbd -xsendevent -text '\[Control_L]y'"
    Mod4 + y 
"/usr/bin/xvkbd -xsendevent -text '\[Control_L]a'"
    Mod4 + a 
"/usr/bin/xvkbd -xsendevent -text '\[Control_L]s'"
    Mod4 + s 
"/usr/bin/xvkbd -xsendevent -text '\[Control_L]o'"
    Mod4 + o 
"/usr/bin/xvkbd -xsendevent -text '\[Control_L]f'"
    Mod4 + f 
for awesome 3.5
awful.key({ modkey,           }, "c",     function () awful.util.spawn("copypaste.sh copy") end),
awful.key({ modkey,           }, "v",     function () awful.util.spawn("copypaste.sh paste") end),
awful.key({ modkey,           }, "x",     function () awful.util.spawn("xvkbd -xsendevent -text '\\[Control_L]x") end),
awful.key({ modkey,           }, "z",     function () awful.util.spawn("xvkbd -xsendevent -text '\\[Control_L]z'") end),
awful.key({ modkey, "Shift"   }, "z",     function () awful.util.spawn("xvkbd -xsendevent -text '\\[Control_L]\\[Shift_L]\\[Z]'") end),
awful.key({ modkey,           }, "y",     function () awful.util.spawn("xvkbd -xsendevent -text '\\[Control_L]y'") end),
awful.key({ modkey,           }, "a",     function () awful.util.spawn("xvkbd -xsendevent -text '\\[Control_L]a'") end),
awful.key({ modkey,           }, "s",     function () awful.util.spawn("xvkbd -xsendevent -text '\\[Control_L]s'") end),
awful.key({ modkey,           }, "o",     function () awful.util.spawn("xvkbd -xsendevent -text '\\[Control_L]o'") end),
awful.key({ modkey,           }, "f",     function () awful.util.spawn("xvkbd -xsendevent -text '\\[Control_L]f'") end),

This article is a translation of the original post at habrahabr.ru/post/266375/
If you have any questions regarding the material covered in the article above, please, contact the original author of the post.
If you have any complaints about this article or you want this article to be deleted, please, drop an email here: sysmagazine.com@gmail.com.

We believe that the knowledge, which is available at the most popular Russian IT blog habrahabr.ru, should be accessed by everyone, even though it is poorly translated.
Shared knowledge makes the world better.
Best wishes.

comments powered by Disqus