How I change keyboard layouts from the command-line? For example, I want to switch from English to Hebrew. I'm able to do it from lightdm with the little icon in the corner of my screen.
You can find all the different keymaps in the following locations:
/usr/share/keymaps/i386/ /usr/share/kbd/keymaps/i386/ /usr/share/X11/xkb/symbols/
To change the keyboard layout (e.g. to Spanish) in the Linux command line, type the following command:
To make these changes system wide, assuming you’re using Ubuntu, you can use the following:
sudo dpkg-reconfigure console-setup
There seems to be no standard CLI tool, but xkb-switch is a small open-source tool to do the job. Just tested it with Ubuntu 16.04.4 LTS and it works flawelessly.
$ xkb-switch # display current group ua $ xkb-switch -l # list groups us ua $ xkb-switch -s us # switch to a named group $
This looks better than the other solutions because
setxkbmap <name> ruins all options, and
setxkbmap -layout <name>, while it preserves the options, it still fails to change the taskbar indicator nor the
grp_led if set.
LayoutLoopCount (e.g. it is set to less than the total number of layouts listed in
LayoutList), like this:
LayoutList=us,ua,th,de LayoutLoopCount=2 Options=grp_led:scroll,grp:rwin_switch,compose:menu,lv3:ralt_switch, nbsp:level2,grp:rctrl_toggle,misc:typo
xkb-switch would only let you switch between the layouts in current group.
Invoke at screen lock
I noticed your comment:
Right before my screensaver turns on, I want to switch to English so that it will be easy to type my password when I get back in.
This was precisely my goal, so here's the answer:
To do that, you only need wrap it into a script hooking to
dbus-monitor like discussed here.
Using gsettings in 18.04+ with Gnome, you can both add add keyboard layouts and switch between them from the command line or from the keyboard layout switcher on the desktop.
1.Present keyboard layouts and options:
$ gsettings list-recursively org.gnome.desktop.input-sources org.gnome.desktop.input-sources show-all-sources false org.gnome.desktop.input-sources xkb-options ['terminate:ctrl_alt_bksp'] org.gnome.desktop.input-sources per-window false org.gnome.desktop.input-sources current uint32 1 org.gnome.desktop.input-sources mru-sources @a(ss)  org.gnome.desktop.input-sources sources [('xkb', 'es'), ('xkb', 'no')]
2.Adding keyboard layouts:
$ gsettings set org.gnome.desktop.input-sources sources "[('xkb', 'us'), ('xkb', 'no'),('xkb','gr')]" $ gsettings get org.gnome.desktop.input-sources sources [('xkb', 'es'), ('xkb', 'no'), ('xkb', 'gr')]
3.Changing the current keyboard layout from the command line:
$ gsettings set org.gnome.desktop.input-sources current 1
As we now have 3 different languages, we have three choices from 0 to 2, with 1 being Spanish, 2 being Norwegian and 3 being Greek. Thus, setting current to 1 above makes Norwegian the current keyboard layout.
4.Listing all possible keyboard layouts:
5.Changing the keyboard layout with a keyboard shortcut:
A keyboard shortcut is preconfigured for changing the keyboard layout. The present shortcut:
$ gsettings get org.gnome.desktop.wm.keybindings switch-input-source ['<Super>space']
A new shortcut can be set by using gsetting set, but take care not to use a shortcut that is already in use. To list all shortcuts in use:
$ gsettings list-recursively org.gnome.desktop.wm.keybindings org.gnome.desktop.wm.keybindings cycle-windows-backward ['<Shift><Alt>Escape'] org.gnome.desktop.wm.keybindings minimize ['<Super>h'] org.gnome.desktop.wm.keybindings move-to-workspace-7 @as  org.gnome.desktop.wm.keybindings move-to-workspace-8 @as  org.gnome.desktop.wm.keybindings move-to-workspace-9 @as  org.gnome.desktop.wm.keybindings move-to-monitor-left ['<Super><Shift>Left'] ...
For Ubuntu 18.04 + Gnome the other answers did not work for me, but this did:
gdbus call --session --dest org.gnome.Shell \ --object-path /org/gnome/Shell \ --method org.gnome.Shell.Eval \ "imports.ui.status.keyboard.getInputSourceManager().inputSources.activate()"
The argument in "inputSources" is the index of the keyboard layout, the same as for gsettings solution.
Found it on linux StackExchange.