Custom X11 keymaps for (mostly) fun and (zero) profit

I recently finished reading the webcomic Homestuck. One of the running gags within the comic is that some characters have a typing quirk, where their text is transformed in some way, such as all caps, replacing the letter 'B' and syllables with a sound similar to "eight" with the numeral eight, or using thematically appropriate puns. One character in particular types in all caps with the letter "A" replaced with the number four, the letter "I" replaced with the number one and the letter "E" replace with the number three.

For the novelty value, I reasoned that it would probably be reasonably straightforward to come up with an X11 keyboard layout to implement that particular typing quirk. X11 (or at least the Xorg implementation) supports loading several keyboard layouts into the server and then switching between them on the fly. A fellow Homestuck reader copied and modified the default US keyboard layout to produce a working prototype layout, which I trimmed down to obtain this.

The fun part is actually loading the keyboard layout into the X server. It's sufficient to simply copy the layout spec file to /usr/share/X11/xkb/symbols/terezi, which can then be loaded using setxkbmap(1) using an incantation similar to setxkbmap -option grp:alt_space_toggle us,terezi, which loads the us and custom terezi keyboard mappings into the X server and binds Alt-space to toggle between keyboard mappings.

This does, however, involve modifying system files, which means that the custom mapping might get clobbered by system updates -- for me it's preferable to keep any local customisations within my home directory, so I created a .xkb directory (with a hierarchy mirroring that of /usr/share/X11/xkb) and copied the layout file to ~/.xkb/symbols/terezi.

Looking through the manual page for setxkbmap(1), there is mention of an -I option for appending a directory to the path in which to search for layouts and rules. At first glance, this looks like it might be an easy solution to the problem, however the manual page notes that "setxkbmap converts its arguments to names of XKB configuration files... then sends these file names to the server where xkbcomp has to compose a complete keyboard map using files which the server has". setxkbmap(1) doesn't actually compile the layout description itself; instead it performs RPC with the X server to get the server to do it on its behalf.

Therefore, an extra step is required to do this manually, by way of xkbcomp(1). setxkbmap(1) is run to compose a layout description (without communicating with the X server), which is then piped to xkbcomp(1) which compiles the layout description and loads it into the X server. The complete incantation looks something like this (note the lack of any whitespace between the -I option and its argument): setxkbmap -I$HOME/.xkb -print -option grp:alt_space_toggle us,terezi | xkbcomp -I$HOME/.xkb - $DISPLAY.

The end result is that 1 C4N NOW TYP3 L1K3 TH1S ON D3M4ND. It's not exactly useful, but it's certainly entertaining.