DEV Community

szabi
szabi

Posted on • Updated on

Custom keyboard layout in GNU/Linux

I generally like to use english keyboard layout for programming, makes it easier to get to the special characters. However, since I'm a hungarian, from time-to-time would like to use accented characters as well. E.g. á, é, ű, ú, ő...

In GNU/Linux to change keyboard layout isn't the easiest to do. As far as I remember (from about 10 years ago) in Windows there is a graphical app that helps you with this. In GNU/Linux this is all via text files.

Here are the steps I've done:

  1. add a new section in the us symbol file - /usr/share/X11/xkb/symbols/us:

    partial alphanumeric_keys
    xkb_symbols "euro-hu" {
    
        include "us(basic)"
        name[Group1]= "English (US, with Hun)";
    
        include "eurosign(5)"
    
        include "level3(ralt_switch)"
    
        key <AD07> { [            u,            U,        uacute,           Uacute ] };
        key <AD03> { [            e,            E,        eacute,           Eacute ] };
        key <AD08> { [            i,            I,        iacute,           Iacute ] };
        key <AD09> { [            o,            O,        oacute,           Oacute ] };
        key <AD10> { [            p,            P,    odiaeresis,       Odiaeresis ] };
        key <AC01> { [            a,            A,        aacute,           Aacute ] };
        key <AE10> { [            0,   parenright,    odiaeresis,       Odiaeresis ] }; // 0 ) ö Ö
        key <AE11> { [        minus,   underscore,    udiaeresis,       Udiaeresis ] }; // - _ ü Ü
        key <AE12> { [        equal,         plus,        oacute,           Oacute ] }; // = + ó Ó
        key <AD11> { [  bracketleft,    braceleft,  odoubleacute,     Odoubleacute ] }; // [ { ő Ő
        key <AD12> { [ bracketright,   braceright,        uacute,           Uacute ] }; // ] } ú Ú
        key <AC10> { [    semicolon,        colon,        eacute,           Eacute ] }; // ; : é É
        key <AC11> { [   apostrophe,     quotedbl,        aacute,           Aacute ] }; // ' " á Á
        key <BKSL> { [    backslash,          bar,  udoubleacute,     Udoubleacute ] }; // \ | ű Ű
        key <LSGT> { [    backslash,          bar,  udoubleacute,     Udoubleacute ] }; // \ | í Í
    };
    
  2. the new keyboard variant should also be noted in the following four files as
    well:

    1. /usr/share/X11/xkb/rules/base.xml:

      <variant>
        <configItem>
          <name>euro</name>
          <description>English (US, euro on 5)</description>
        </configItem>
      </variant>
      <variant>
        <configItem>
          <name>euro-hu</name>
          <description>English (US, with Hun)</description>
        </configItem>
      </variant>
      <variant>
        <configItem>
          <name>intl</name>
          <description>English (US, intl., with dead keys)</description>
        </configItem>
      </variant>
      
    2. /usr/share/X11/xkb/rules/base.lst:

      euro            us: English (US, euro on 5)
      euro-hu         us: English (US, with Hun)
      intl            us: English (US, intl., with dead keys)
      
    3. /usr/share/X11/xkb/rules/evdev.xml - same way as base.xml

    4. /usr/share/X11/xkb/rules/evdev.lst - same as base.lst

  1. Restart the system and the new keyboard layout should be available - for gnome - in the Regional & Language setting's Input Sources section.

If you wish to dig deeper in the topic there is a good documentation about this over on a polish programmer's site Michał Kosmulski's "Creating custom keyboard layouts for X11 using KB" article.

There is also an option to make keyboard layout modifications with Xmodmap, but I've had some bad experiences with it. Namely it was slow to load the config, so rather do it via XKB. Possible that I was doing something bad with Xmodmap... but we will never no that now. :D

Originally I've posted this on https://szab.it/posts/custom-keyboard-layout-in-gnu_linux/

Top comments (2)

Collapse
 
dmfay profile image
Dian Fay

Have you tried a compose key? Double-acute ő for me is right alt, =, o without any layout changes. Many other symbols are already defined too, like € (right alt, e, =), ł (right alt, /, l), or ß (right alt, s, s). The custom layout might be faster if you're typing a lot of text though.

Collapse
 
szabi profile image
szabi

Hello Dian,

I've not tried the XCompose option, other solution I've tried was to also use the US international keyboard layout, with that you can do ó by pressing the "" and then "o", but that was quite slow, also was trouble some as when I wanted to actually write "", then had to press it twice.

XCompose does sounds like a good solution maybe I'll try it on my next install :)