익명 06:27

How do I get the current keyboard layout in Ubuntu in a format such as 'us'?

How do I get the current keyboard layout in Ubuntu in a format such as 'us'?

I'm using i3 window manager, Ubuntu 20.04.
I ran the following commands to set up two keyboard layouts:

setxkbmap -layout us,tr
setxkbmap -option 'grp:alt_shift_toggle'

This allows me to toggle between the two keyboard layouts us and tr with alt-shift.
I want to get a short output, us or tr, from the command line that tells me which keyboard layout I'm currently on.

The following command gives me us as the output, even when I've switched to tr.
setxkbmap -print | awk -F"+" '/xkb_symbols/ {print $2}'

The full output of setxkbmap -print is below:

xkb_keymap {                                                                                           
        xkb_keycodes  { include "evdev+aliases(qwerty)" };                                             
        xkb_types     { include "complete"      };                                                     
        xkb_compat    { include "complete"      };                                                     
        xkb_symbols   { include "pc+us+tr:2+inet(evdev)+group(alt_shift_toggle)+capslock(escape)"};    
        xkb_geometry  { include "pc(pc105)"     };                                                     
};                                                                                                     

How can I get the current keyboard layout in the command line?



Top Answer/Comment:

You could use xkblayout-state:

xkblayout-state is a small command-line program to get/set the current XKB keyboard layout.

The command to use is:

xkblayout-state print "%s"
상단 광고의 [X] 버튼을 누르면 내용이 보입니다