Switch between languages in Linux with xkb. Write in multiple languages with same keyboard.

Polyglot Tux
Image: Polyglot Tux (License: CC-BY-ND-SA Marcelo Canina)

How to set up multiple keyboard layouts from console.

Published:
Last modified:

Overview

A guide for Linux polyglots to use Cyrillic letters, Chinese characters, Latin letters and any other language in Linux, with the ability to easily switch between them in an X11 graphical environment or console.

The X11/Xorg server uses XKB, the X Keyboard Extension, to control the available keyboard layouts.

If we want to use many languages with the same keyboard, we will have to address four main topics: model, layout, variant and options.

To specify the different languages, we use the command setxkbmap which sets the keyboard using the X Keyboard Extension.

It is used like: setxkbmap [-model xkb_model] [-layout xkb_layout] [-variant xkb_variant] [-option xkb_options] so the in each topic we will see how to fulfill each parameter.

1. Current configuration

You can see your current configuration with setxkbmap -query or for further details setxkbmap -print -verbose 10 .


$ setxkbmap -query
rules:      evdev
model:      pc105
layout:     us

In Debian/Ubuntu systems the default keyboard layout is described in /etc/default/keyboard1.


$ cat /etc/default/keyboard
 KEYBOARD CONFIGURATION FILE

 Consult the keyboard(5) manual page.

XKBMODEL="pc105"
XKBLAYOUT="us"
XKBVARIANT=""
XKBOPTIONS=""

BACKSPACE="guess"

At this stage we pay particular attention to the current “physical” keyboard model.

XkbModel selects the keyboard model. This has an influence only for some extra keys your keyboard might have. The safe fallback are pc104 or pc105. But for instance laptops usually have some extra keys, and sometimes you can make them work by simply setting a proper model.

2. Languages

Select which keyboard layouts are needed, i.e.: the languages you will use.

XkbLayout selects the keyboard layout. Multiple layouts may be specified in a comma-separated list, e.g. if you want to quickly switch between layouts.

For a complete list of keyboard layouts and its descriptions check /usr/share/X11/xkb/rules/base.lst.

The layout name is often the typical two letter country code. e.g.: for Russian: ru.

3. Variants

For each layout, a variant can be chosen.

XkbVariant selects a specific layout variant. For instance, the default sk variant is qwertz, but you can manually specify qwerty, etc.

The full list of variants can also be seen at /usr/share/X11/xkb/rules/base.lst.

For example you can choose:

  • For a Russian layout, a phonetic variant is available, which will make it writing Russian in a Latin/US keyboard very intuitive.

    ! variant
    ...
      phonetic_winkeys ru: Russian (phonetic, with Win keys)
    ...
    

    And to see specifically the mappings it has, consult the proper layout file, e.g. for Russian (ru): /usr/share/X11/xkb/symbols/ru and locate the xkb_symbols "phonetic". It will have all the key mappings like: key <LatT> { [ Cyrillic_te, Cyrillic_TE ] }; where it specifies that the Latin T key will be the Cyrillic version: т.

The full list of variants available can be seen online at: https://manpages.ubuntu.com/manpages/disco/man7/xkeyboard-config.7.html

4. Switch between layouts

XkbOptions contains some extra options (comma-separated). Used for specifying layout switching, notification LED, compose mode etc.

To switch between the keyboard layouts we rely on the options parameter of XKB.

It already comes with a list of possible key combinations that will be switching between all the specified layouts listed in /usr/share/X11/xkb/rules/base.lst, starting with grp: and ending with toggle.


$ grep "grp:.*toggle" /usr/share/X11/xkb/rules/base.lst
grp:toggle           Right Alt
  grp:lalt_toggle      Left Alt
  grp:caps_toggle      Caps Lock
  grp:shift_caps_toggle Shift+Caps Lock
  grp:alt_caps_toggle  Alt+Caps Lock
  grp:shifts_toggle    Both Shift together
  grp:alts_toggle      Both Alt together
  grp:ctrls_toggle     Both Ctrl together
  grp:ctrl_shift_toggle Ctrl+Shift
  grp:lctrl_lshift_toggle Left Ctrl+Left Shift
  grp:rctrl_rshift_toggle Right Ctrl+Right Shift
  grp:ctrl_alt_toggle  Alt+Ctrl
  grp:alt_shift_toggle Alt+Shift
  grp:lalt_lshift_toggle Left Alt+Left Shift
  grp:alt_space_toggle Alt+Space
  grp:menu_toggle      Menu
  grp:lwin_toggle      Left Win
  grp:win_space_toggle Win+Space
  grp:rwin_toggle      Right Win
  grp:lshift_toggle    Left Shift
  grp:rshift_toggle    Right Shift
  grp:lctrl_toggle     Left Ctrl
  grp:rctrl_toggle     Right Ctrl
  grp:sclk_toggle      Scroll Lock
  grp:lctrl_lwin_toggle Left Ctrl+Left Win

The full list of options available can be found online at: https://manpages.ubuntu.com/manpages/disco/man7/xkeyboard-config.7.html .

5. Testing

To test it works as desired, we specify the command and try to switch between layouts.

For example for English and Russian (phonetic) keyboards switching with both CTRL keys pressed at the same time: setxkbmap -model pc105 -layout us,ru -variant ,phonetic -option grp:ctrls_toggle.

Then we press both CTRL and start typing in Русский.

6. Persisting Changes

setxkbmap changes the layout for the current X session only.

To keep changes in the next reboot we can choose between:

  • Manually changing the configuration files:
    • xinitrc or
    • xprofile.
  • Using the command localectl which automatically saves the new configuration.

6.1 Manual

Create the X11 configuration file /etc/X11/xorg.conf.d/00-keyboard.conf with the new data:

Section "InputClass"
        Identifier "system-keyboard"
        MatchIsKeyboard "on"
        Option "XkbLayout" "us,ru"
        Option "XkbModel" "pc105"
        Option "XkbVariant" ",phonetic"
        Option "XkbOptions" "grp:ctrls_toggle"
EndSection

6.2 Automatic

locatectl automatically creates/updates the required configuration file /etc/default/keyboard (in Ubuntu) or /etc/X11/xorg.conf.d/00-keyboard.conf.

It is used like: $ localectl [--no-convert] set-x11-keymap layout [model [variant [options]]]

Following the above example:


$ localectl set-x11-keymap us,ru pc105 ,phonetic grp:ctrls_toggle
$ cat /etc/default/keyboard
XKBMODEL=pc105
XKBLAYOUT=us,ru
XKBVARIANT=,phonetic
XKBOPTIONS=grp:ctrls_toggle
BACKSPACE=guess

7. Graphical indicator

Finally we add an X11 keyboard indicator and switcher.

gxkb is an utility that switchs the language selecting it from a menu, and shows an icon with the current active layout.

If you change the layout with the above combination key, gxkb also updates and show the current layout.

screenshot 1

To install it in Debian/Ubuntu: sudo apt-get install gxkb.

Keyboards layout references

To generate any graphical representation of the keyboard layout, use gkbd-keyboard-display -l "<language_code> or xkbprint.

Examples:

  • for English US version: gkbd-keyboard-display -l us
  • for English US with altgr-intl variant: gkbd-keyboard-display -l “us(altgr-intl)“
Variants for each language can be found at /usr/share/X11/xkb/symbols/

Each letter on the keyboard can have up to 4 symbols associated. They are defined in /usr/share/X11/xkb/symbols/ for each language in the form of key <KEYCODE> { [ symbol1, symbol2, symbol3, symbol4 ] };.

Each symbol can be accesed with:

  • unmodified
    • <letter>
    • press a letter without any other modificator
  • shift
    • press SHIFT + <letter>
    • access the upper left letter symbol definition
  • altgr
    • press ALT gr + <letter>
    • access the bottom right letter symbol definition
  • shift+altgr
    • press SHIFT + ALT gr + <letter>
    • access the upper left letter symbol definition

altgr stands for Alt Graph and is the right alt key. “Is a modifier key used to type characters that are unusual for the locale of the keyboard layout”. 2

Example key layout and how to access each symbol:

SHIFT SHIFT + ALT gr
without modifiers ALT gr

Arab

Arab keyboard layout without variants.

Code: ar.

Arab layout

Brazilian

Brazilian portuguese keyboard layout.

Code: br.

Brazilian layout

English (US)

English US keyboard layout.

Code: us(altgr-intl).

English with altgr international layout

French

French keyboard layout.

Code: fr.

French layout

Italian

Italian keyboard layout.

Code: it.

Italian layout

Russian

Russian keyboard main layout is called ЙЦУКЕН3, the keyboard with the most popular Cyrillic letters layout.

Code: ru.

Russian layout

Russian phonetic

Russian phonetic variant layout lets you use a common US English layout (qwerty) but typing each letter phonetic equivalent in Cyrillic.

Code: ru(phonetic).

Russian phonetic layout

Spanish

Spanish keyboard layout.

Code: es.

Spanish layout

References


  1. man keyboard ↩︎

  2. https://en.wikipedia.org/wiki/AltGr_key ↩︎

  3. https://en.wikipedia.org/wiki/JCUKEN ↩︎

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


Setup from console to switch between multiple languages in Linux. Use Cyrillic, Pinyin and Latin characters.

Clutter-free software concepts.
Translations English Español

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·