blog:linux_adapt_keyboard

Linux adapt keyboard

Long, long ago in a galaxy far, far away, I attempted starting a blog. It didn't last, but one of the things I posted was about this very subject. When I deleted the blog, I kept the articles. So here's that one. I apologise for the length.

A few years back I started to get what I think was RSI in my hands. I never got it officially diagnosed so I can't be sure, but all the symptoms seemed clear. It was worse when I was typing, and worst in my little fingers.

Think about typing. How much work do the little fingers do compared to the others? As well as having their own letters, they also work many of the punctuation characters and the shift (and control) keys. I found I was often having to stretch my hand to type characters and this was putting a lot of strain on my little fingers.

It's even worse when typing LaTeX documents. A quick scan through a thirty-page paper reveals that the five most typed characters are:

  space 15525
  e 7266
  \ 6834
  o 5476
  t 5470

There then follow a few more lowercase letters, in 14th and 15th place are the parentheses (worryingly not the same number of each - must have some half-open intervals in there). 22nd is the underscore, 24th and 25th are the curly braces (quick check: the same number this time) with just over a thousand occurrences. The first number, 1, is way down the list with only 362 appearances.

By the way, if you want to generate this list, there are probably more elegant ways but here's my two-minute hack:

~% cat paper.tex| \
       perl -lne 'while ($_) {
            $_ =~ s/(.)//;
            $count{$1}++};
            END {
               @chars = sort {$count{$b}  $count{$a}} keys %count;
               while (@chars) {
                  $c = shift @chars;
                  print "$c $count{$c}";
               }
           }'

The backslash key is often hard to stretch to, the curly braces usually require shift (or Alt-Gr on some international keyboards). That's a lot of work for what is, as far as catching mammoths is concerned, something pretty useless.

My solution was to modify the keyboard. No, not with a hammer. With a nifty little program called xmodmap. This is a UNIX program which allows you to modify what the keys on the keyboard actually do. I use it to put the backslash where the semi-colon is (after all, who uses a semi-colon these days?), swap the curly braces and square brackets, and swap the numbers with their symbols (so pressing '3' produces '#' and 'Shift+3' produces '3').

Unfortunately, this method isn't very portable and I have to set it up for each machine. The problem is that it is a translation table from what the keyboard currently does to what you want it to do, so you first have to know what it currently does. However, it's fairly simple to explain how to set it up.

Suppose you want to put the backslash where the semi-colon is. First you need to find the keycode for the semi-colon. There are two ways to do this. Firstly, from a terminal run a program called xev. When you press a key in its window, it tells you lots of information about it in the terminal - including the keycode. The other way to do it is to run xmodmap -pke. This produces a list of all the current assignments from which you can read off the keycode for the semi-colon.

~% xmodmap -pke | grep semicolon
keycode   47 = semicolon colon oslash Oslash

(Yeah, I'm on a Norwegian keyboard.) Now you just need to remap that:

~% xmodmap -e 'keycode 47 = backslash colon oslash Ooslash'

Lo and behold! Keyboard modified.

Three things to note. Save the initial output of xmodmap -pke since if everything goes wrong typing:

~% xmodmap original_list

will reset it (though that might be difficult if you've reset all the keys! In that case, log out and log back in again). Secondly, to save you typing in all those commands every time, you can put them all in a file called, say, .xmodmap and put a line xmodmap ~/.xmodmap in your startup file. Gnome actually goes looking for these files and asks if you want to load them so you don't need to put them in your startup file. The final point is that you might want to consider having different keyboards for different tasks. I have a keyboard for writing LaTeX documents and a “normal” one for everything else; switching between them is easy using “hot keys”.

Several years after figuring this out, I discovered that I was alone neither in the problem nor in the solution. Greg Kuperberg has also written about this, though his solution uses XKB rather than xmodmap. Your mileage may vary.

Here's the actual layout:

` 1 2 3 4 5 6 7 8 9 0 _ + ~ ! @ # $ % ^ & * ( ) - =

TAB Q W E R T Y U I O P [ ]

  q w e r t y u i o p { }

Ctrl A S D F G H J K L : “ |

   a s d f g h j k l \ ' /

Shift > Z X C V B N M < > ? Shift

    < z x c v b n m , . ;

In addition, the “weird” keys along the bottom are mapped to various modifiers which give me access to other characters (most usually, øæå, as I'm in Norway). This is actually done with a two-stage xmodmaprc: the first stage changes my Scandinavian keyboard into something I'm a little more used to; the second stage does the TeX-related changes. I have one hotkey that starts Emacs and changes the keyboard all in one go!

Ref: http://tex.stackexchange.com/a/1985/13660

~~LINKBACK~~ ~~DISCUSSION~~

  • blog/linux_adapt_keyboard.txt
  • Last modified: 2012/04/17 11:10
  • by brb