17

For usage in a bash script, I need to get the gnome keyboard layout the user is currently using. For example if the user sets its keyboard layout to en-us , I need a bash command that prints me this.

How can I get that information?

Update:

setxkbmap -query is unfortunatelly not working. Below is the ouput with the en (first command) and the de (second command) layout activated. Switching keyboard layout seems to be have some relation with gnome session configuration

setxkbmap -query 
rules:      evdev
model:      pc105
layout:     us,de
variant:    ,
options:    terminate:ctrl_alt_bksp,lv3:ralt_switch,grp:alts_toggle

setxkbmap -query
rules:      evdev
model:      pc105
layout:     us,de
variant:    ,
options:    terminate:ctrl_alt_bksp,lv3:ralt_switch,grp:alts_toggle

Update2:

setxkbmap -print #with en-us layout
xkb_keymap {
    xkb_keycodes  { include "evdev+aliases(qwerty)" };
    xkb_types     { include "complete"  };
    xkb_compat    { include "complete"  };
    xkb_symbols   { include "pc+us+de:2+inet(evdev)+level3(ralt_switch_for_alts_toggle):1+level3(ralt_switch_for_alts_toggle):2+group(alts_toggle)+level3(ralt_switch)+terminate(ctrl_alt_bksp)"    };
    xkb_geometry  { include "pc(pc105)" };
};
setxkbmap -print #after switching to german layout
xkb_keymap {
    xkb_keycodes  { include "evdev+aliases(qwerty)" };
    xkb_types     { include "complete"  };
    xkb_compat    { include "complete"  };
    xkb_symbols   { include "pc+us+de:2+inet(evdev)+level3(ralt_switch_for_alts_toggle):1+level3(ralt_switch_for_alts_toggle):2+group(alts_toggle)+level3(ralt_switch)+terminate(ctrl_alt_bksp)"    };
    xkb_geometry  { include "pc(pc105)" };
};
2
  • Sadly it's not bash, but using libxklavier, here's a [python example][1] to do what your asking: [1]: cgit.freedesktop.org/libxklavier/tree/tests/test_gi.py
    – balloons
    Jul 2, 2012 at 16:00
  • 1
    <rant>xkb is an undocumented hell! These multiple layout definitions don't work reliably; for me they suddenly switched at random times and after suspend it was randomly determined which of the defined layouts was in effect and -- what's worse -- independently for each keyboard attached. Yes, xkb is actually able to have different layouts for different keyboards but this is not documented anywhere as far as I know and therefore the only thing this feature does is annoy us because we can't control it. xkb, seriously, don't get me started!</rant>
    – Christian
    Jul 5, 2012 at 16:54

6 Answers 6

10
+25

According to a similar question on Stackoverflow, the following should do the trick:

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

I did could not verify it, as I currently have no *nix machine with X available (I'm not home)...

7
  • 1
    Running setxkbmap -print returns the following. I don't think this helps the OP. xkb_keymap { xkb_keycodes { include "evdev+aliases(qwerty)" }; xkb_types { include "complete" }; xkb_compat { include "complete" }; xkb_symbols { include "pc+us+inet(evdev)" }; xkb_geometry { include "pc(pc105)" }; };
    – balloons
    Jul 2, 2012 at 15:49
  • So you currently have "us" layout active? That is what above statement, given your input, would produce as output (only those two letters: "us"). How does it look when you switch to a different layout?
    – Izzy
    Jul 2, 2012 at 16:19
  • 2
    Thanks for your answer. Unfortunatelly this is not doing the trick. Both times I get us. I have edited my question to show you the ouput.
    – ftiaronsem
    Jul 2, 2012 at 17:18
  • was worth a try. You could try to specify the -v parameter (multiple times), which increases verbosity and thus gives more information. Should work for both, -query as well as -print. As I have no multilang setup, I cannot tell whether it provides the detail you need -- but with 3 times -v output here is split into "applied rules" and "keymap". The latter seems to be the complete information, so the first part could have the "currently active part".
    – Izzy
    Jul 2, 2012 at 19:48
  • thanks again, but unfortunatelly this did not work out either. The same output before and after the layout change. Independently of the verbosity.
    – ftiaronsem
    Jul 3, 2012 at 16:22
6

For Ubuntu 17.10 or later

In Ubuntu 17.10, with GNOME, the current gsettings value is not changed when you switch input source. Instead there is a mru-sources key which lists the most recently used input sources.

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

The first source in that list is the current one, so a oneliner to get the current layout may look like this:

gsettings get org.gnome.desktop.input-sources mru-sources | sed -r "s/\S*\s'([^']+).*/\1/"

Please note that this answer does not apply if you use Unity on an Ubuntu 17.10 system. With Unity it keeps working as previously.

3

For Ubuntu 13.04 and lower

You can use xkblayout-state tool. See README.md file for description, compilation, installation and usage.

The following command will do exactly what you want:

xkblayout-state print "%s"

For Ubuntu 13.10 and higher

Ubuntu 13.10 came with some good improvements in this sense, and you can use the following simple bash function:

get_current_xkblayout () {
      current_input_nr=$(gsettings get org.gnome.desktop.input-sources current | \
          awk '{ print $NF }')
      shift=$(( 2 * ( $current_input_nr + 1 )))
      gsettings get org.gnome.desktop.input-sources sources | tr -d "\',[]()" | \
          awk -v cur="$shift" '{ print $cur }'
}

The following commands also works in 13.10:

setxkbmap -query | awk -F"(,|[ ]+)" '/layout:/ { print $2 }'

or:

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

Using the terminal, I've run a test changing between 'pt' and 'us', and after every change, I've collected the keyboard layout being used with success:

Get the active keyboard layout

setxkbmap -print | grep xkb_symbols | awk -F"+" '{print $2}'
  • Print the configuration: setxkbmap -print
  • Collect the line that matters: grep xkb_symbols
  • gets the string after the first "+" sign: awk -F"+" '{print $2}'

The output having 'us' layout active is: us


Swith between layouts

sudo setxkbmap -option grp:alt_shift_toggle pt

Pass where it reads 'pt', the language code to switch to.

enter image description here

Note: I'm using Gnome on Ubuntu 12.04 (Precise Pangolin)

2
  • Thanks for your answer. The problem seems to be that users tend to switch between layouts by using task-bar applet. It works if one is switching layout from cmd, but it does not work, if the user switches with the task bar applet.
    – ftiaronsem
    Jul 3, 2012 at 16:24
  • @ftiaronsem, You haven't mentioned that part before :) I'll look into it and update my answer if possible!
    – Zuul
    Jul 3, 2012 at 16:37
1

Just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command below.

setxkbmap -query

This is what you should see

enter image description here

3
  • thanks for your prompt answer, unfortunatelly this is not working. Here is the ouput (first time with en layout, second with de):
    – ftiaronsem
    Jun 25, 2012 at 18:38
  • due to formatting pasted as question edit...
    – ftiaronsem
    Jun 25, 2012 at 18:38
  • 1
    thanks again for your prompt reply. This is inded what I am seeing, but it does not tell me, whether I selected the german or the american layout. Or have I missed something?
    – ftiaronsem
    Jun 25, 2012 at 19:39
0

For Ubuntu 13.10 or Later

(Not sure if it works in previous releases)

current_layout=gsettings get org.gnome.desktop.input-sources current | awk '{print $2}'
gsettings get org.gnome.desktop.input-sources sources | grep -Po "'[[:alpha:]]+'\)" | sed -ne "s/['|)]//g;$(($current_layout+1))p"

To see more option:

gsettings list-recursively org.gnome.desktop.input-sources

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .