Maciej Janicki's website

2019-05-24 x11 ergonomy

Making your keyboard layout more ergonomic

There is no doubt that the keyboard is the most efficient input device to interact with your computer. I use the keyboard for pretty much everything, including web browsing, switching between windows and arranging them, writing e-mails etc. However, many programs require combinations with a modifier key (especially Ctrl) to trigger some functionality. Because of the placement of the modifier keys on the keyboard, repeated pressing of such combinations can be inconvenient and require weird twists and stretches. In this post, I show how to remap certain keys to a much more ergonomic placement.

The home row and the placement of modifier keys

As I live in Germany, I normally use German keyboards with the so-called ISO layout, meaning an extra key between Z and left Shift. However, I use the Polish keyboard layout, which is basically the same as the US layout. Thus, a typical keyboard I work on looks like this:

The home row and other easily reachable keys

The keys marked in green are the so-called “home row” - the position in which your fingers should normally rest. They are obviously the easiest keys to reach, as you don’t have to move your fingers at all in order to reach them. Additionally, the keys marked in orange are also quite easy to reach - they require only a slight finger movement.

Now, I mentioned the combinations involving the modifier keys. Let’s have a look on where they are placed:

The both Alt keys are not that bad. They are placed right next to the space bar, so you can press them with your thumbs and the position of the other fingers is almost unaffected. But the other ones, especially the frequently needed Ctrl, are a catastrophe. They are apparently supposed to be pressed by the little finger, which is the weakest of all. Moreover, doing this requires either twisting the wrist outwards, or crooking the little finger, or most often both. This introduces a tension in your hand, requires you to use more strength, and the bend of the wrist changes the position of other fingers, making the letter keys harder to reach. You may laugh, but if you have to do this non-stop for 10 hours a day or longer, it is a serious problem. This is the reason why some people find programs that make extensive use of such combinations, like especially Emacs, unusable.

(I myself don’t use Emacs, but mostly for other reasons. I’ve found that the modkey combinations are not as awful as they seem, if you follow the advice in this post.)

A simple solution

A simple solution to this problem is to place the Ctrl keys near to the home row, so that they are reachable without leaving the natural position:

On the left, pretty much all keyboard have CapsLock in this position. This key is large, wonderfully reachable and completely useless. Remapping it to Ctrl is thus a commonly found advice and I strongly recommend it.

However, I also need an equally convenient right Ctrl key. The key that occupies the right position is the backslash/pipe (\|). It is not used very often, so it doesn’t harm to remap it to Ctrl and find another place for the backslash. On my keyboard, the perfect place for that is the ISO key (marked in blue), which is useless in the Polish/US layout, because its characters - greater/less sign - are also found elsewhere.

That’s it! This slight remapping will make Ctrl-combinations ergonomic. A further idea would be to remap Tab to a modifier key (for example, Super), but it didn’t work well for me. Tab is slightly smaller than CapsLock, so it is not as easy to reach as it seems, and we have a muscle memory for pressing it in some contexts. Moreover, I don’t really need Super, since my window manager (DWM) uses the left Alt as a modifier key.

Some Vim users map Tab to Esc, but for Vim I have a better idea, which I’ll save for another post.

There is also another type of keyboard, called ANSI, on which the Return key is bar-shaped and the backslash is placed above it. I don’t use this type currently and I’m not sure whether remapping backslash to Ctrl would be a good idea there.

xmodmap

On the X window system, such key remapping is ridiculously easy to achieve using the program xmodmap. You describe the desired mappings in a text file, typically called ~/.xmodmaprc, and then simply call:

$ xmodmap ~/.xmodmaprc

It is good to have this command auto-run on system start, for example by putting it into your .xinitrc.

My .xmodmaprc, which does the remapping described above, looks like this:

clear Lock
keycode 66 = Control_L
keycode 51 = Control_R
add Control = Control_L Control_R
keycode 94 = backslash bar backslash bar dead_grave dead_breve

Each physical key on the keyboard is identified by a number (called keycode). Using the keycode command, you can reassign those codes to the “meanings” (keysyms) recognized by X. Remapping CapsLock is a bit tricky, because you have to deactivate the “lock” that it triggers. This is what the first line does. The fourth line adds (or re-adds) the concept of Control modifier, which consists of pressing the left or right Ctrl. I am not sure why it has to be explicitly stated as it should be set up by default, but without this line the “new” Ctrl keys didn’t work.

The last line maps the ISO key to what the backslash key (51) previously was. As you see, you can assign multiple keysyms to a key. The first one is the key pressed without any modifiers, the second one with Shift. The third and fourth one seems to be with some weird modifier key that nobody uses (and with/without Shift), so it is virtually always mapped to the same as the first and second. Finally, the fifth/sixth pair is achieved by additionally pressing AltGr.

Needless to say, the manpage for xmodmap explains all the available commands. It is not a lot in this case.

There are different ways of finding out what the keycode of a key is. Firstly, you can use the xev program. It opens a window and reports all the events for this window (among others, keypresses) to the terminal. If you press a key with this window focused, you will get some information about the pressed key, including its keycode.

Another method is to call:

$ xmodmap -pk

which prints out the table of current mappings.

One final advice

There is a reason why I insisted on having both a left and a right Ctrl in a convenient place. People sometimes tend to use only one of them - typically the left one - and press combinations like Ctrl+S, Ctrl+C with one hand. I think it is a bad habit. I recommend to always press the modifier key with the opposite hand as the letter key. This allows you to use less strength and move your hands less - especially stay in the home row. The same applies to Shift.