The old-fashioned and modern ways to remap keys in X (some notes)

December 7, 2015

As I mentioned recently, I've been remapping my keyboard in X for a very long time. In all of this time, I've been using the traditional and now old fashioned way of doing this, namely xmodmap. Xmodmap is now a deprecated mechanism, although it still works; these days you usually want to be remapping keys with setxkbmap.

You might wonder why X has two different programs for this. As you could guess, it's the usual reason; X added a new system for handling keyboard remapping without removing the old one (because X never removes anything). Xmodmap uses the old system, which I believe has been in X since at least the start of X11, while setxkbmap uses the new system, the X KeyBoard extension.

(Well, 'new' is relative here. XKB apparently dates from 1996, almost two decades ago. I was a little bit surprised to find that out, since I hadn't heard of it myself until recently. This is probably the hazard of having become set in my X ways a very long time ago.)

I'm not the right person to write up a big evaluation of the two mechanisms against each other, because up until recently I hadn't looked into XKB and setxkbmap at all (and even now I only know a bit). So from my limited perspective, the big difference between the two is that xmodmap offers relatively simple and direct low-level control for weird operations while setxkbmap gives you simple ways to do most of what you often want, provided that you can work out the right arguments.

To show what I mean by this, here is more or less the xmodmap magic needed to swap Backspace and Delete keys and to make CapsLock act as a Control key:

keysym BackSpace = Delete
keysym Delete = BackSpace

remove Lock = Caps_Lock
keycode 0x42 = Control_L
add Control = Control_L

You put this in a file and then run 'xmodmap FILE' and magic happens.

By contrast, here is the setxkbmap command needed to make CapsLock act as a Control key and make the right Windows key act as Compose:

setxkbmap -option ctrl:nocaps -option compose:rwin

You will notice that I have not shown you how to swap Backspace and Delete with setxkbmap. That's because that particular change is not among the predefined changes that XKB supports and once you venture outside of those predefined changes, things become rather complicated.

(The predefined changes themselves are documented in the xkeyboard-config manpage. The files that define what these options and so on mean are all found under /usr/share/X11/xkb; see for example /usr/share/X11/xkb/symbols/ctrl. If you look at these files, you'll see that XKB configuration is, well, intricate.)

Because setxkbmap fits my needs right now (now that I'm not swapping Delete and Backspace) and because it's apparently the more approved-of way these days, I've switched from xmodmap to setxkbmap. I probably would not have changed over if I was doing anything that setxkbmap couldn't handle through its existing options for -options; while more sophisticated things are possible, they appear to be a lot of work.

The ArchLinux wiki has some nice references on Keyboard configuration in Xorg, xmodmap, and the X KeyBoard extension. While I accumulated a bunch of additional links while I was looking into setxkbmap, I'm not going to put any of them here because I don't know enough to assess their quality and whether they're up to date.

Written on 07 December 2015.
« The (peculiar) freedom of having a slow language
What I like web templating languages for »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Mon Dec 7 01:45:34 2015
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.