šŸ“œ ā¬†ļø ā¬‡ļø

XKB: reconfigure the keys for a loved one

One fine day bothers you to press Shift to get the ~ character instead of ` .
Bothers to reach Esc, while using the CapsLock key to use RARE.
Bothers to move the brush down and press it with a weak little finger, or, God forbid, reach for it with your thumb.
It bothers to climb PgUp, PgDn, Home and End, moving your hand each time just to squander the page down.
Bothers every time you make a typo, look at the keyboard with anger in search of the Backspace button.



Today we will learn how to permanently get rid of these torments.
')

Why is this all about?


To press less keys. To stretch your fingers less to those keys that are often pressed. To hands less tired. To make it easier to remember what is where. To print faster in the end.

Many layouts for printing (Dvorak, Colemak, etc.) greatly improve the location of the keys, putting them closer to those places where you need to reach less. But they completely forget about modifiers, specials. characters and command keys (Enter, Backspace, Tab, Esc, Delete). And we will think about how to improve the status quo.

The article will go from the simplest changes to more interesting and deep tuning. And, as in the end of good films, I will leave the reader to think about 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 for the keyboard layout.

First steps cripples


Standard layout needs to be modified by itself. This can be done in many ways. I don’t think that someone is using exotic ibus, Fcitx, SCIM or UIM, so I’ll assume that everyone has a standard X Keyboard extension, XKB. It is worth noting that in the future on the desktop Wayland, XKB, is likely to remain responsible for the keyboard, as well as in Xorg. If suddenly there are brave souls who are already working with XWayland, share your impressions of whether what I have described works for you. I can’t say anything about Canonical’s alternative Wayland Mir, except that they are unlikely to take on rewriting the keyboard interaction subsystem.

The first thing that comes to mind is to use some kind of graphical utilities. To be honest, I missed this step, and immediately used xmodmap. Below, I will tell you what is wrong with this approach and how to do it correctly.

Xmodmap allows, for example, swapping ; and :, this is especially useful for those who program in a modern language that 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 not to lose the letter and when exchanging.

But such, for example, swaps ~ and ` :
 $ xmodmap -e "keycode 49 = asciitilde grave Cyrillic_io Cyrillic_IO" 

Switch Layouts


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

Esc vs CapsLock


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

The same can be done a little easier:
  setxkbmap -option caps:escape 

Or even like this, keeping CapsLock in place of the old Escape in case of correspondence in a raised voice:
  setxkbmap -option caps:swapescape 

Whence these mysterious parameters, we will find out a little later.

Little flowers


Probably, I will surprise you if I say that what we did before was not entirely correct. setxkbmap is still nothing, but xmodmap appeared before XKB, and lives only for compatibility. Moreover, he knows nothing about half the keys, his syntax and principle of operation is more than mysterious, he translates keycode and keysym, and in some cases back. There is no point in understanding this, because at some point he will die off, and all your tricks will need to be repeated, only with the help of another utility, and you will become frustrated and in spite of everything you will start typing with two index fingers.

So, it all works up to one beautiful 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


Do it all right with xkbcomp. The name of the utility, as well as man to it, give little information about what it does. Allegedly translates textual descriptions of the keyboard layout into a binary format. 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 people on the Internet advise you to modify the files contained in / usr / share / X11 / xkb / symbols, namely us, pc and ru. Do not do this. These are files that affect the entire system at once. For other users (if you suddenly have one), and if you are messing with something there, then typing, even the login and password, is more difficult. You don’t even need to put the modified files there, and you don’t need to write anything in evdev.xml.

Important: if you suddenly do something wrong, then rebooting the X will help you. There is a simpler 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 know the current layout settings, we need to do this before all the manipulations:
 $ setxkbmap -layout us,ru -print 

At the output we 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)" }; }; 

Let's write all this into a file, and follow the instructions of the XDG , and put it in ~ / .config / xkb / my.

In this file, we are most interested in the xkb_symbols line, the rest will remain unchanged. Let's 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 <AC10> { [ colon, semicolon ] }; key <TLDE> { [ 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 the letter , the symbol is printed:, but not;, and when you press Y, a tilde is printed ~, and with Shift everything happens the other way around, then we are on the right track.

It is worth noting that there is no need to bother with Cyrillic_zhe Cyrillic_ZHE, everything works and so.

Go ahead. What did we do? We ran setxkbmap -option with a parameter. Let's see what it 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)" }; 

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

It should be noted that the semicolon after the include is not needed, in contrast to the semicolon in other places of this configuration file.
If you look closely, you get the understanding that these two ways of recording 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 visual.

What does this mean? We load our layout, we say that we have qwerty (xkb_keycodes), that we have 104 keys (xkb_geometry), and we redefine characters. At the same time, in the symbols section we include entirely several configuration files, pc, us, ru. Where do they lie, can I look at them? Naturally, they all lie in / usr / share / X11 / xkb / symbols. Seeing them is useful, especially if you are thinking of doing 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 like this:
 partial hidden modifier_keys xkb_symbols "swapescape" { key <CAPS> { [ Escape ] }; key <ESC> { [ Caps_Lock ] }; }; partial hidden modifier_keys xkb_symbols "escape" { key <CAPS> { [ Escape ] }; }; 

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

Why we simply do not do the partial, and we do not include it? To my shame, I am ready to admit that I tried to do it by descriptions, but it did not work out for me.

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

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


The 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 in one case:
 setxkbmap -I$HOME/.config/xkb -symbols my -print 

But, alas, in the case of the transfer of this output to xkbcomp, it gives an error.

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

PS Habrayuzer kodx found out how to do it right .

Berries


I don't want to just escape to CapsLock. I want to control too.
How? How is this possible? It's time for magic.

First, let's make it so that when you click on CapsLock, you get LCTL by adding this to the xkb_symbols section:
  replace key <CAPS> { [ Control_L ] }; replace key <LCTL> { [ 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 it:
  replace key <LCTL> { [ VoidSymbol ] }; 

Now we need a xcape's eye , which allows us to assign a second action to the modifier. So, if it is pressed in combination with some alphanumeric key, 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 action of a 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 pressed the left Ctrl and kept it pressed for more than 1000 ms, then Escape will not work. Excellent option for indecisive.

Among the shortcomings of this approach, we can call the fact that Escape will work not by pressing, but by releasing the key. To some, this delay is not terrible, but to someone it will be critical. And someone will compromise.

Bonus


For any useful, but rare characters, there is such a thing as Compose Key , which, after pressing and releasing it, allows you to type some sequence that corresponds to some Unicode character, which will appear on the screen. For example, the sequence of clicks "Compose oo" will display the symbol ° . And ā€œCompose cccpā€ will issue ☭ . Funny? There are a lot of characters, and the combinations are chosen so that it is easy to fill them in, and in most cases the order of pressing is not important, for example, ā€œCompose e =ā€ and ā€œCompose = eā€ both give out € . A full list of standard combinations can be found here , and you can also assign your own ones in the ~ / .XCompose file.
And you can make the right Ctrl work as a Compose by adding the following line to our file:
  include "compose(rctrl)" 

On the road


I deliberately do not publish my config entirely, so that the reader wants to experiment. Here are a couple of ideas for you on the road:
- by pressing F , make HJKL work like normal arrows;
- make the left half of the keyboard shift the register only with the right shift, and the right half with the left. This is very useful for hands, although not familiar ( hint in the comments);
- make it so that the left Shift does not have to be clamped, but that it was enough to press and release once and then, for example, press the 5 key, so that the % symbol is typed (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 sit down to someone else for the keyboard, and everything is not right at once, then I will say this: when transferring from Bentley to help a friend bring a Lada to the house, you also swear. All the more pleasant to sit back in Bentley.

Print with pleasure and take care of yourself!

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


All Articles