XKB: reconfigure the keys for yourself beloved

    One fine day bothers to press Shift so that the symbol ~ appears instead of ` .
    Tired of reaching up to Esc, while using the CapsLock key, use RARE.
    Tired of moving the brush down and pressing Ctrl / Cmd / Win with a weak little finger, or, God forbid, reach for them with your thumb.
    Tired of climbing for PgUp, PgDn, Home and End, moving your hand each time, just to scroll down the page.
    Bored every time you make a typo, angrily look at the keyboard in search of the Backspace button.



    Today we will learn how to get rid of these torments forever.

    Why is this all?


    To press less keys. To stretch your fingers less to the keys that we often press. To make your hands less tired. To make it easier to remember where. To print faster, after all.

    Many layouts for printing (Dvorak, Colemak, etc.) greatly improve the layout of the keys, putting them closer to those places where you need to stretch less. But at the same time they completely forget about modifiers, specials. characters and command keys (Enter, Backspace, Tab, Esc, Delete). And we will think about how to improve the current state of affairs.

    In the article, we will go from the simplest changes to a more interesting and deep tuning. And, as at the end of good films, I will leave the reader to think what to do next, and I will not give a ready-made recipe for everything. In the end, there are no ideal and universal solutions, and there are no comrades to layout keyboards.

    The first steps of the cripple


    The standard layout needs to be modified for yourself. There are many ways to do this. I don’t think that anyone here uses exotic ibus, Fcitx, SCIM or UIM, so I’ll assume that everyone uses the standard X Keyboard extension, XKB. It is worth noting that in the upcoming Wayland desktop, the XKB will most likely remain responsible for the keyboard, as well as in the Xorg. If suddenly there are daredevils who already work with XWayland, share your impressions if the described by me works for you. I can not say anything about Canonical’s alternative Wayland Mir, except that they are unlikely to rewrite the keyboard interaction subsystem.

    The first thing that comes to mind is to use some kind of graphical utility. To be honest, I skipped this step, and immediately took advantage of xmodmap. Below I will tell you what is wrong with this approach and how to do it right.

    Xmodmap allows, for example, swap layers ; and :, this is especially useful for those who program in a modern language, which no longer requires a mandatory semicolon at the end of a line. Even in JavaScript, this is no longer necessary. And this simple command helps:
    $ xmodmap -e "keycode 47 = colon semicolon Cyrillic_zhe Cyrillic_ZHE"
    

    You need to run it from .xsession or .xinitrc. The last two arguments in quotes will help you not to lose in the exchange of letters F and F .

    But this one, for example, will interchange ~ and ` :
    $ xmodmap -e "keycode 49 = asciitilde grave Cyrillic_io Cyrillic_IO"
    

    Layout Switching


    Do you often have to use Alt? If suddenly not, but I, I noticed, have to do this only in menu-rich interfaces, such as Gimp or LibreOffice, then it can be used as a switch right Alt, known as AltGr:
    $ setxkbmap -layout us,ru -option grp:toggle
    

    Esc vs CapsLock


    Many who often use Vim, or use vi shortcuts in the console, and who swear a little on forums, prefer to have Escape on the CapsLock key:
     $ xmodmap -e "clear lock"
     $ xmodmap -e "keycode 0x42 = Escape"
    

    The same can be made a little simpler:
      setxkbmap -option caps:escape
    

    Or even like this, keeping CapsLock in the place of the old Escape in case of correspondence on elevated tones:
      setxkbmap -option caps:swapescape
    

    Where these mysterious parameters come from, we will find out a little later.

    Flowers


    I will probably surprise you if I say that what we did before was not quite right. setxkbmap is still nothing, but xmodmap appeared before XKB, and lives only for compatibility. Not only that, he does not know anything about half the keys, the syntax and principle of his work is more than cryptic, he translates keycode and keysym, and in some cases back. Understanding this makes no sense, because it will die off at some point, and all your tricks will need to be repeated, only with the help of another utility, and you will be upset and, in spite of everything, will again print with two index fingers.

    So, all this works until one wonderful moment, until you begin to understand that there are a lot of necessary keys, and the fingers that lie on the home line (ASDF JKL;) do not reach everyone.
    For example, I often use the key with the designation Win, which is a Mod4 modifier. It is located on my keyboard is not very convenient, namely between the left Alt and Ctrl.

    The right way


    It’s right to do all this with xkbcomp. The name of the utility, like man to it, give little information about what it does. Allegedly translates textual descriptions of the keyboard layout in binary format. But in fact it also loads them. Let's try to understand how to do what we have already done, only with its help.

    Remember, many on the Internet advise modifying files that are in / usr / share / X11 / xkb / symbols, namely us, pc and ru. Do not do this. These are files acting on the entire system at once. For other users (if you suddenly have one), and if you mess with something there, then typing, even a username and password, is harder. You should not even put modified files there, and you do not need to write anything in evdev.xml.

    Important:if you suddenly do something wrong, then rebooting X's will help you. There is an easier way, you can run the setxkbmap command without parameters, and it will reset almost all of your settings to the default layout.

    First of all, we need to find out the current layout settings, we need to do this before all the manipulations:
    $ setxkbmap -layout us,ru -print
    

    The output will get something like this:
    xkb_keymap {
      xkb_keycodes  { include "evdev+aliases(qwerty)" };
      xkb_types     { include "complete" };
      xkb_compat    { include "complete" };
      xkb_symbols   { include "pc+us+ru:2+inet(evdev)" };
      xkb_geometry  { include "pc(pc104)" };
    };
    

    We will write all this into a file, and we will follow the instructions of XDG , and put it in ~ / .config / xkb / my.

    In this file, we are most interested in the xkb_symbols line, the rest will be left unchanged. Expand the curly braces, and change this line to the following:
    xkb_keymap {
      xkb_keycodes  { include "evdev+aliases(qwerty)" };
      xkb_types     { include "complete" };
      xkb_compat    { include "complete" };
      xkb_geometry  { include "pc(pc104)" };
      xkb_symbols "my" {
        include "pc+us+ru:2+inet(evdev)"
        key  { [ colon, semicolon ] };
        key  { [ asciitilde, grave ] };
      };
    };
    

    Well, cross your fingers, and load this configuration:
    $ xkbcomp $HOME/.config/xkb/my $DISPLAY
    

    We try. If, when you press the key on which you have written the letter Ж, the symbol is printed:, but not;, and when you press Е, the tilde ~ is printed, and with Shift everything happens the other way around, then we are on the right track.

    It’s worth noting that you don’t have to bother with Cyrillic_zhe Cyrillic_ZHE right there, everything works like that.

    Move on. What did we do? We ran setxkbmap -option with a parameter. Let's see what he changes.
    $ setxkbmap -print | grep symbols
      xkb_symbols   { include "pc+us+ru:2+inet(evdev)" };
    $ setxkbmap -option caps:escape
    $ setxkbmap -print | grep symbols
      xkb_symbols   { include "pc+us+ru:2+inet(evdev)+capslock(escape)" };
    

    Well, now it seems to be clear that the layout is changing by the inclusion of some options.
    In our my file, add the following line to the xkb_symbols section, immediately after the already existing include:
        include "capslock(escape)"
    

    It is worth noting that the semicolon after include is not needed, unlike the semicolon in other places of this configuration file.
    If you look carefully, you realize that these two recording methods are identical:
        include "pc+us+ru:2+inet(evdev)"
        include "capslock(escape)"
    

        include "pc+us+ru:2+inet(evdev)+capslock(escape)"
    

    In this case, the first is much more obvious.

    What does this mean? We load our layout, say that we have qwerty (xkb_keycodes), that we have 104 keys (xkb_geometry), and redefine the characters. At the same time, in the symbol section, we include entirely several configuration files, pc, us, ru. Where do they lie, is it possible to look at them? Naturally, they all lie in / usr / share / X11 / xkb / symbols. It’s useful to see them, especially if you are planning to do something more ambitious than we have already done.
    Well, where are those files that determine that CapsLock and Escape need to be swapped? Yes there. It looks something like this:
    partial hidden modifier_keys
    xkb_symbols "swapescape" {
        key  { [ Escape ] };
        key   { [ Caps_Lock ] };
    };
    partial hidden modifier_keys
    xkb_symbols "escape" {
        key  { [ Escape ] };
    };
    

    What is partial? This is a piece of layout that can be included in another layout. In some configuration files, generous authors even added comments. But not at all.

    Why don't we just make our partial, and don't turn it on? To my shame, I’m ready to admit that I tried to do this according to the descriptions, but it didn’t work out for me.

    How to do it in theory
    In the layout file, you do not need to put the whole xkb_keymap, only the xkb_symbols section is enough:

      xkb_symbols "my" {
        include "pc+us+ru:2+inet(evdev)"
        key  { [ colon, semicolon ] };
        key  { [ asciitilde, grave ] };
      };
    


    Download should go with the command:
    setxkbmap -I$HOME/.config/xkb my -print | xkbcomp - $DISPLAY
    

    Alas, the output of setxkbmap does not change from any parameters, except for one case:
    setxkbmap -I$HOME/.config/xkb -symbols my -print
    

    But, alas, in the case of transferring this output to xkbcomp, it throws an error.

    Some people advise instead of $ DISPLAY to write $ {DISPLAY %%. *}

    PS Habrauser kodx figured out how to do it right .

    Berries


    I don’t want to just Escape to CapsLock. I want Control too.
    How? How is this possible? The time has come for magic.

    First, make sure that when you click on CapsLock, you get LCTL by adding this to the xkb_symbols section:
        replace key  { [ Control_L ] };
        replace key  { [ Caps_Lock ] };
    

    The second line is needed in order to get rid of the bad habit of pressing the old left Ctrl. You can slightly soften this:
        replace key  { [ VoidSymbol ] };
    

    Now we need the woodpecker's eye, the xcape utility , which allows you to assign the second action to the modifier. So, if it is pressed in combination with some alphanumeric key, then it works as a modifier, and if it is pressed and released alone, it works somehow differently, as we want. To battle!
    $ xcape -t 1000 -e "Control_L=Escape"
    

    xcape is demonized by the woodpecker's eye . It is also worth noting the interesting parameter -t 1000. No, he does not call the terminator, he says that if we still pressed the left Ctrl and kept it pressed for more than 1000 ms, then Escape will not work. Great option for indecisive.

    One of the drawbacks of this approach is that Escape will work not by pressing, but by releasing the key. This delay is not terrible for some, but it will be critical for someone. And someone will compromise.

    Bonus


    For all sorts of useful, but rare characters, there is such a thing as Compose Key , which allows you to press a certain sequence after pressing and releasing it, which corresponds to some kind of Unicode character that appears on the screen. For example, a sequence of keystrokes “Compose oo” will display ° . And Compose cccp will give выда . Is it funny There are a lot of symbols, and the combinations are chosen so that it is easy to capture them, and in most cases the order of pressing is not important, for example, “Compose e =” and “Compose = e” both give . A complete list of standard combinations can be found here , and you can also assign your own in the ~ / .XCompose file.
    And to make the right Ctrl work as Compose, you can add the following line to our file:
        include "compose(rctrl)"
    

    On the road, on the road


    I deliberately do not publish my entire config so that the reader wants to experiment. Here's a couple of ideas for you on the road:
    - by pressing the F key , make the HJKL work like regular arrows;
    - make the left half of the keyboard shift the register with only the right Shift, and the right with the left. This is very useful for the hands, although not familiar ( tooltip in the comment);
    - make sure that the left Shift does not have to be clamped, and that it was enough to press and release it once, and then press, for example, key 5 , so that the % symbol is printed (hint: Latch);
    - hang two different modifiers on one key

    Come up, share. I will be glad to hear interesting ideas.

    PS If suddenly someone says that then you’ll sit down at someone else’s keyboard and everything’s not right at once, then I’ll say this: when transferring from Bentley to help a friend drive a Lada to the house, you also swear. All the more pleasant to sit back in Bentley.

    Print with pleasure and take care!

    Also popular now: