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

command lineconsolekeyboard-layout

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 ?

Best Answer

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.