How to get current gnome keyboard layout from terminal?

17,749

Solution 1

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)...

Solution 2

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.

Solution 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}'

Solution 4

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)

Solution 5

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

Share:
17,749

Related videos on Youtube

engineerX
Author by

engineerX

Updated on September 18, 2022

Comments

  • engineerX
    engineerX over 1 year

    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)" };
    };
    
    • balloons
      balloons almost 12 years
      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
    • illya
      illya almost 12 years
      <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>
  • engineerX
    engineerX almost 12 years
    thanks for your prompt answer, unfortunatelly this is not working. Here is the ouput (first time with en layout, second with de):
  • engineerX
    engineerX almost 12 years
    due to formatting pasted as question edit...
  • engineerX
    engineerX almost 12 years
    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?
  • balloons
    balloons almost 12 years
    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)" }; };
  • David Tod
    David Tod almost 12 years
    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?
  • engineerX
    engineerX almost 12 years
    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.
  • David Tod
    David Tod almost 12 years
    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".
  • engineerX
    engineerX almost 12 years
    thanks again, but unfortunatelly this did not work out either. The same output before and after the layout change. Independently of the verbosity.
  • engineerX
    engineerX almost 12 years
    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.
  • Zuul
    Zuul almost 12 years
    @ftiaronsem, You haven't mentioned that part before :) I'll look into it and update my answer if possible!
  • David Tod
    David Tod almost 12 years
    Sorry. Then I'm out. Good luck to you -- hopefully someone else comes up with something usable...
  • SnowBEE
    SnowBEE over 11 years
    The answer on SO is wrong, and it doesn't print the current layout if there are more than one.