4

Is there a way to change the currently active keyboard layout in Cinnamon, from command line?

The setxkbmap solution that most of the guides suggest, doesn't quite work as it is supposed to: If it is called, the language indicator in Cinnamon itself dissapears, and the existing combinations configured in Cinnamon do not work any more. To restore this functionality, one has to go to the settings and re-enable any of the keyboard layout setting.

Additionally, the way setxkbmap seems to change the layout is of a nature of being lower level than Cinnamon seems to like: for example if you change a layout to a non-latin one, using setxkbmap, the default key combinations in the whole system stop working. (easily tested with Ctrl-C). This never happens with the native layout switching from within Cinnamon. It appears that setxkbmap operates at a lower level than what most applications expect.

Is there a way to change the layout the native, Cinnamon (or gnome, since it's based on it?) way and keep the keyboard combinations working?

(Another solution usually proposed is gsettings set org.gnome.desktop.input-sources current 0, which does not work either in current Cinnamon (nothing happens)).

I have of course, tested all the possible indexes, up to 10, even though I have only 3 layouts added currently. The mentioned variable does not seem to change either when the layouts are changed manually. I have also tried the similar variable that exists in Cinnamon environment, org.cinnamon.desktop.input-sources, but it does not affect anything either (in exactly the same way as the gnome version).

4
  • What about gsettings set org.gnome.desktop.input-sources current 1 ? Commented Oct 24, 2017 at 17:01
  • I have of course tested different values, I have updated my question.
    – Cray
    Commented Oct 25, 2017 at 16:05
  • At a terminal prompt, one may use the SUPER-SPACE shortcut to cycle through your keyboard layouts. Will this work for you? Or must it be a solution for scripts, alone?
    – TheGeeko61
    Commented Oct 25, 2017 at 16:29
  • Well, if that does not work for switching layout, Cinnamon seems to do it its own way, differently from Unity and GNOME. I'm not able to tell how. Commented Oct 25, 2017 at 18:50

2 Answers 2

3

The best solution currently is to use this external program (xkb-switch) that accepts command line arguments and script it into xbindkeys or something similar.

Although unofficial, external, and written in a compilable language, it is actually pretty easy to install since it has almost no dependencies.

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

$ xkb-switch --help

Usage: xkb-switch -s ARG            Sets current layout group to ARG
       xkb-switch -l|--list         Displays all layout groups
       xkb-switch -h|--help         Displays this message
       xkb-switch -v|--version      Shows version number
       xkb-switch -w|--wait [-p]    Waits for group change and exits
       xkb-switch -W                Infinitely waits for group change
       xkb-switch -n|--next         Switch to the next layout group
       xkb-switch [-p]              Displays current layout group

Use as:

xkb-switch -s us
xkb-switch -s de
etc...

This solution:

  • Works from command line.
  • Is fast, invocation of this program does not introduce any delay (as opposed to switching layout setxkbmap).
  • Correctly switches the built-in layout indicator in the tray icon area. (The indicator does not dissappear or stops working, as with setxkbmap.)
  • Does not screw up keymap on the lockscreen, preventing entering the right password if the wrong layout was chosen before locking (as does setxkbmap).
  • Does not modify the main key combinations like Ctrl-C, Ctrl-V: they still work with the same keys even in different layouts (being different from, well you get the idea, setxkbmap).
  • Honors all the Cinnamon layout mechanics, like the setting of either using the same layout for all windows or individual layout per-window. (Take a guess if setxkbmap does that?)

The installation instructions are on the Github.

1

You can use something like:

setxkbmap ru,us

For the layout in Russian to preserve all global hotkeys like Ctrl + C.

The idea seen here: https://unix.stackexchange.com/questions/316998/how-to-change-keyboard-layout-in-gnome-3-from-command-line.

What changes in comparison with just a mere setxkbmap ru is that xkb_symbols is expanded from { include "pc+ru+inet(evdev)" }; to { include "pc+ru+us:2+inet(evdev)" }; (check it with setxkbmap ru -print).

So, though not sure, I guess when used ru alone, it resets all additional mappings and sets the layout as-is. With ru,en it sets the layout to Russian, but also sets all the necessary interpretations for global keys, taken from the US layout.

You must log in to answer this question.

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