Posted on

Custom Ubuntu Keyboard Layouts

Sometimes a programmer just has to have certain keys in certain places.

For instance, I like my number keys and symbol key switched, so I don’t have

to press shift to type symbols.

Here’s how I created a custom keyboard layout in Ubuntu:

In the file /usr/share/X11/xkb/symbols/us, I added the following section, thus modifying my default layout:

default
partial alphanumeric_keys modifier_keys
xkb_symbols "basic" {
name[Group1]= "USA";

// Alphanumeric section
key <TLDE> {        [asciitilde,        grave            ]        };
key <AE01> {        [    exclam,        1                ]        };
key <AE02> {        [        at,        2                ]        };
key <AE03> {        [ numbersign,       3                ]        };
key <AE04> {        [    dollar,        4                ]        };
key <AE05> {        [   percent,        5                ]        };
key <AE06> {        [ asciicircum,      6                ]        };
key <AE07> {        [ ampersand,        7                ]        };
key <AE08> {        [  asterisk,        8                ]        };
key <AE09> {        [ parenleft,        9                ]        };
key <AE10> {        [ parenright,       0                ]        };
key <AE11> {        [     minus,        underscore       ]        };
key <AE12> {        [     equal,        plus             ]        };
key <AD01> {        [         q,        Q                ]        };
key <AD02> {        [         w,        W                ]        };
key <AD03> {        [         e,        E                ]        };
key <AD04> {        [         r,        R                ]        };
key <AD05> {        [         t,        T                ]        };
key <AD06> {        [         y,        Y                ]        };
key <AD07> {        [         u,        U                ]        };
key <AD08> {        [         i,        I                ]        };
key <AD09> {        [         o,        O                ]        };
key <AD10> {        [         p,        P                ]        };
key <AD11> {        [ bracketleft,      braceleft        ]        };
key <AD12> {        [ bracketright,     braceright       ]        };
key <AC01> {        [         a,        A                ]        };
key <AC02> {        [         s,        S                ]        };
key <AC03> {        [         d,        D                ]        };
key <AC04> {        [         f,        F                ]        };
key <AC05> {        [         g,        G                ]        };
key <AC06> {        [         h,        H                ]        };
key <AC07> {        [         j,        J                ]        };
key <AC08> {        [         k,        K                ]        };
key <AC09> {        [         l,        L                ]        };
key <AC10> {        [ semicolon,        colon            ]        };
key <AC11> {        [ apostrophe,       quotedbl         ]        };
key <AB01> {        [         z,        Z                ]        };
key <AB02> {        [         x,        X                ]        };
key <AB03> {        [         c,        C                ]        };
key <AB04> {        [         v,        V                ]        };
key <AB05> {        [         b,        B                ]        };
key <AB06> {        [         n,        N                ]        };
key <AB07> {        [         m,        M                ]        };
key <AB08> {        [     comma,        less             ]        };
key <AB09> {        [    period,        greater          ]        };
key <AB10> {        [     slash,        question         ]        };
key <BKSL> {        [ backslash,        bar              ]        };
// End alphanumeric section
};

Each line in this file tells Ubuntu what character to type when a particular key is pressed. For instance, in a line like

key  {        [ numbersign,       3                ]        };

…we can see that key “AE03” will produce the number sign by default, and “3” when shift is held down. This is reverse of the normal layout, since it reflects my customization. The original line would read:

key  {        [ 3,        numbersign                ]        };

All the spaces being used to line everything up are just to help make things look nice and neat, and could just as easily be left out.

After modifying this file, I went to “System > Preferences > Keyboard” and clicked the “Layouts” tab. I added the layout, which was found under the United States country. I had some problems with the layout switching arbitrarily, or applying in one program but not others, so I disabled all other layouts.

It’s as easy as that. Remember to make a backup of your original /usr/share/X11/xkb/symbols/us file, and hack away!