20

I have always had trouble with understanding the way keyboard mapping and related things are put together in Linux.

When things break, it makes my blood boil if I have to sift through endless outdated mailing list and forum posts to find THAT one command or inputrc line that fixes my problem.

There are classic problems like backspace not working in vim, or Ctrl + arrows in bash until you switch terminal type. Or a problem I've encountered recently, where in fresh Debian install @ key actually prints ", and " prints @ (wrong keyboard layout?)

Just looking at files and tools doesn't help too much. inputrc? xmodmap? setxkbmap? console-setup? Where do I get started to actually understand how it works so I don't have to resort to trying someone's dubious commands to fix my keyboard problems?

1
  • Also take a look at xkeycaps, it lets you see and change xmodmap settings interactively.
    – starblue
    Aug 21, 2010 at 13:19

3 Answers 3

16

This is much more complicated than it should be, but here's my stab at it.

At the most basic level, the kernel knows how to recognize keyboard devices and it understands the concept of a console keymap. This is the simplest way to configure your keyboard, and there's only one variable to consider, but these settings only affect your keyboard input on the Linux text console.

Once you get into Xorg, things get a bit more complicated, but it does actually make a kind of sense. Xorg has several specific notions which are very important to understand.

  • Xorg talks directly to the keyboard device, bypassing the kernel and ignoring the keymap specified on the console.
  • Xorg allows you to set some specific options for your keyboard in the xorg.conf file, although current Linux distributions try their best to run without any xorg.conf file at all. Depending on your version of Xorg, you may need to specify your XkbLayout inside of an InputClass section (for newer versions of Xorg) or an InputDevice section (for older releases).
  • There are several specific layers of interpretation in Xorg when a key is pressed.
  1. At the most basic level, each keypress event is represented by a numeric keycode.
  2. The XkbModel combined with the XkbLayout are used to associate a given keycode with an actual key event. For example, on my keyboard, keycode 50 generates Shift_L.
  3. You can optionally specify multiple XkbLayouts for a keyboard, and then use the XkbOptions (full list usually stored in /usr/share/X11/xkb/rules) to configure how to switch layouts. (This is useful if, say, you're trying to teach yourself Dvorak, or you want to alternate between English and Russian keyboard layouts).
  4. Each keycode can be interpreted in multiple ways, depending on which modifiers are being held. xmodmap lets you see what key events will be sent based on which modifiers are being held.
3

These days, the console and X can actually work together.

I don't know if this completely standardized yet, but in Debian Unstable (and presumably other distros with up to date Xorg and console tools), /etc/default/keyboard allows you to use X-style syntax to setup the keyboard in the console and in X - at least if you are using console-tools and console-setup. The rest is as clee described.

0

There are classic problems like backspace not working in vim...

Perhaps you are talking about the behaviour of vim to delete with backspace only the chars you typed in the current edition, and preventing you to do so with characters already present before you entered in editing mode.

That behaviour is changed by setting:

set backspace=indent,eol,start

Not the answer you're looking for? Browse other questions tagged .