How to get keyboard layout (language) in terminal (console, command-line)

15,776

As regards Ubuntu 17.10, I just figured out that the current keyboard layout in a desktop session is saved differently.

I have a Swedish and English (US) layout available. Previously I could do:

$ gsettings get org.gnome.desktop.input-sources sources
[('xkb', 'se'), ('xkb', 'us')]
$ gsettings get org.gnome.desktop.input-sources current
uint32 0

The current value 0 (zero) told me that the current layout was the first layout in the sources list. However, in 17.10 the current value is not altered if I switch layout.

Instead, 17.10 includes the mru-sources key, which lists the most recently used input sources. Hence, when Swedish is my current layout, it looks like this:

$ gsettings get org.gnome.desktop.input-sources mru-sources
[('xkb', 'se'), ('xkb', 'us')]

and if I switch to English it looks like this:

$ gsettings get org.gnome.desktop.input-sources mru-sources
[('xkb', 'us'), ('xkb', 'se')]

So the current layout is simply the first input source in the mru-sources list.

Share:
15,776

Related videos on Youtube

Faither
Author by

Faither

Aka: /\ngel, F8ER, Faither, V.7, V7... We'll get it done. Correctly. With love.

Updated on September 18, 2022

Comments

  • Faither
    Faither over 1 year

    Is it possible to get the current keyboard layout in command-line? I've already tried different ways:

    1. localectl ( status ) etc.
    2. cat /etc/default/locale
    3. cat /etc/default/keyboard
    4. setxkbmap -query
    5. gsettings ...
    6. setxkbmap -print | grep xkb_symbols ( setxkbmap -v | awk -F "+" '/symbols/ {print $2}' )
    7. Tried to install xkblayout-state, but it doesn't want to be installed (make fails)

    So ... all of this show the same output event if keyboard layout was changed to another ... they show us layout. Btw I've checked dozen of topics and no result ... so please don't mark it as a duplicate. Let's find out how to achieve it properly.

    uname -a shows Linux x4 4.4.0-97-generic #120-Ubuntu SMP Tue Sep 19 17:28:18 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

    Distribution is KDE Neon 5.8 which is based on Ubuntu

    Window system is X11

    By the way I've found this: xset -q | grep -A 0 'LED' | cut -c59-67 here. Is it clear way to do this ?


    The outputs of above commands:

    $ localectl status:

    System Locale: LANG=en_US.UTF-8
       LC_NUMERIC=ru_RU.UTF-8
       LC_TIME=ru_RU.UTF-8
       LC_MONETARY=ru_RU.UTF-8
       LC_PAPER=ru_RU.UTF-8
       LC_NAME=ru_RU.UTF-8
       LC_ADDRESS=ru_RU.UTF-8
       LC_TELEPHONE=ru_RU.UTF-8
       LC_MEASUREMENT=ru_RU.UTF-8
       LC_IDENTIFICATION=ru_RU.UTF-8
     VC Keymap: n/a
    X11 Layout: us
    X11 Model: pc105
    

    $ cat /etc/default/locale:

    #  File generated by update-locale                                                                                                                                                 
    LANG="en_US.UTF-8"                                                                                                                                                                 
    LC_NUMERIC="ru_RU.UTF-8"                                                                                                                                                           
    LC_TIME="ru_RU.UTF-8"                                                                                                                                                              
    LC_MONETARY="ru_RU.UTF-8"                                                                                                                                                          
    LC_PAPER="ru_RU.UTF-8"                                                                                                                                                             
    LC_NAME="ru_RU.UTF-8"                                                                                                                                                              
    LC_ADDRESS="ru_RU.UTF-8"                                                                                                                                                           
    LC_TELEPHONE="ru_RU.UTF-8"                                                                                                                                                         
    LC_MEASUREMENT="ru_RU.UTF-8"                                                                                                                                                       
    LC_IDENTIFICATION="ru_RU.UTF-8"
    

    $ cat /etc/default/keyboard:

    # KEYBOARD CONFIGURATION FILE                                                                                                                                                      
    
    # Consult the keyboard(5) manual page.
    
    XKBMODEL="pc105"
    XKBLAYOUT="us"
    XKBVARIANT=""
    XKBOPTIONS=""
    
    BACKSPACE="guess"
    

    $ setxkbmap -query:

    rules:      evdev
    model:      pc101
    layout:     us,ru
    options:    grp:alt_shift_toggle
    

    $ setxkbmap -print | grep xkb_symbols:

            xkb_symbols   { include "pc+us+ru:2+inet(evdev)+group(alt_shift_toggle)"        };
    

    $ setxkbmap -v | awk -F "+" '/symbols/ {print $2}':

    us
    

    $ cat ~/.config/kxkbrc:

    [Layout]
    DisplayNames=,
    LayoutList=us,ru
    LayoutLoopCount=-1
    Model=pc101
    Options=grp:alt_shift_toggle
    ResetOldOptions=true
    ShowFlag=false
    ShowLabel=true
    ShowLayoutIndicator=true
    ShowSingle=false
    SwitchMode=Global
    Use=true
    

    All of these commands were ran two times ( before and after keyboard layout changing ). So no result.


    By the way:

    $ xset -q | grep -A 0 'LED' | cut -c59-67 ( English language is enabled ):

    00000000
    

    $ xset -q | grep -A 0 'LED' | cut -c59-67 ( Russian language is enabled ):

    00001000
    

    So ... as you see this one changes. All in all is it clear way to get current layout ?

  • Faither
    Faither over 6 years
    Thank you for your reply, but gsettings get org.gnome.desktop.input-sources mru-sources gives No such key 'mru-sources'
  • Gunnar Hjalmarsson
    Gunnar Hjalmarsson over 6 years
    @V.7: It may well be a GNOME specific thing. Ubuntu uses GNOME by default in 17.10.
  • Faither
    Faither over 6 years
    I don't use Gnome. KDE Plasma instead.
  • Gunnar Hjalmarsson
    Gunnar Hjalmarsson over 6 years
    @V.7: I know, so I suspected that this wouldn't help you. Found it still motivated to document my 'finding'.
  • Faither
    Faither over 6 years
    Btw Thank you very much as needed @Gunnar Hjalamarsson :)