Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm wondering is there any command line tool, that returns current keyboard layout.

I have XkbLayout set to us, ru.

Update: setxkbmap returns layout settings, not selected layout. E.g.:

$ setxkbmap -print | grep xkb_symbols
xkb_symbols   { include "pc+us+ru:2+inet(evdev)+capslock(grouplock)+terminate(ctrl_alt_bksp)"   };

It will return same result no matter what current layout is.

share|improve this question
    
I am not really good at D-Bus, but it should be easy to track the current keyboard layout using it I think. Although it may not be able to do so if there is no active switch. –  Benjamin Dec 26 '11 at 18:16

7 Answers 7

There is xkb-switch which is described thus:

xkb-switch is a C++ program that allows to query and change the XKB layout state.

https://github.com/ierton/xkb-switch

Or, following nozimica's suggestion, you could use:

setxkbmap -print | awk -F"+" '/xkb_symbols/ {print $2}'

From this thread on the Arch Linux boards: https://bbs.archlinux.org/viewtopic.php?pid=539406

share|improve this answer
1  
Thanks, xkb-switch works fine, but I hope that there was more portable solution –  Andrew Apr 28 '11 at 16:34
    
@Andrew: yes it's kind of odd there is no simple way to get the current layout. –  Benjamin Dec 26 '11 at 17:38

Maybe this is version dependent, but on my machine that uses setxkbmap 1.3.0 the following command works:

setxkbmap -query | grep layout

Note that depending on your need it may be useless to know only the layout : for instance the Dvorak variant of the US layout is quite different than the default QWERTY. The -query option of setxkbmap gives both the layout and the variant, as different fields :

$ setxkbmap -query
rules:      evdev
model:      default
layout:     fr
variant:    bepo
options:    grp:alt_shift_toggle
share|improve this answer
1  
-query was added in setxkbmap 1.2.0 –  alanc Apr 6 '13 at 1:49

Use this to get the code for the current layout:

$(xset -q|grep LED| awk '{ print $10 }')

This might needs to be converted to a form you want, like:

case "$(xset -q|grep LED| awk '{ print $10 }')" in
  "00000002") KBD="English" ;;
  "00001002") KBD="Thai" ;;
  *) KBD="unknown" ;;
esac
share|improve this answer
1  
I get 00000002 even though my layout is "USA Dvorak international". Language is not enough... –  l0b0 Jan 5 '12 at 11:42
2  
It doesn't help if there are three or more layouts. The second and the third layouts give the same value 00001004 on my machine. –  sastanin Dec 19 '12 at 8:37

Yes THERE IS a command line tool that does what you want! I just discovered it 10min ago :)

Look at here: https://github.com/nonpop/xkblayout-state

xkblayout-state print "%s"

does exactly what you want (it doesn't output an end of line, so add ; echo if you need). run the tool without parameters for the help.

share|improve this answer
1  
This is great. Thanks! –  orschiro Nov 21 '13 at 15:21

Another simpler approach, because of fixed positions of the output of the xset -q command, is this:

xset -q | grep -A 0 'LED' | cut -c59-67

It prints 00000002 or 00001002 depending on your current keyboard layout.

share|improve this answer

You can use:

setxkbmap -print | grep xkb_symbols
share|improve this answer
1  
it returns overall keyboard settings, not current layout (us or ru) –  Andrew Apr 28 '11 at 11:17
    
Execute it well, as @jasonwryan states if you analyze carefully that line, into it resides your layout. In my case it is latam. –  nozimica Apr 28 '11 at 14:10
    
jasonwryan version just output a part of string after '+', xkb_symbols value doesn't depend on selected layout, I always get 'us' –  Andrew Apr 28 '11 at 16:33

From Goosfrabaa in the Arch Linux forums:

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

This works correctly here, and prints us(dvorak-intl) (displayed as "USA Dvorak international" in the GNOME keyboard selection menu).

share|improve this answer
1  
It prints only the first layout in the list, not the current one. –  sastanin Dec 19 '12 at 8:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.