85

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.

12 Answers 12

69

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:

loadkeys es

For X:

setxkbmap es

To make these changes system wide, assuming you’re using Ubuntu, you can use the following:

sudo dpkg-reconfigure console-setup
8
  • This has effect on tty1, tty2, etc, but no effect in X. I want to do it in X, the same way that the keyboard switcher in lightdm works.
    – Eyal
    Oct 30, 2012 at 13:21
  • I added a command in the answer, was this what you were looking for?
    – Dr_Bunsen
    Oct 30, 2012 at 13:25
  • YES! Thanks. Also, can you update your answer to mention loadkeys, spelled with an 's'?
    – Eyal
    Oct 30, 2012 at 13:27
  • 2
    yep I will, and no thanks. Just mark as solved so everyone knows.
    – Dr_Bunsen
    Oct 30, 2012 at 13:28
  • for redhat users, following are the valuable pieces: /usr/share/X11/xkb/symbols/ and setxkbmap es Nov 18, 2015 at 11:35
54

From terminal

English to Hebrew and vise versa with Alt + Shift

setxkbmap -option grp:alt_shift_toggle us,il

You can see all locale alias with this command

cat /etc/locale.alias

More info about setxkbmap in manual

man setxkbmap
2
14

Ubuntu 13.10 or Later

This works better then setxkbmap with Gnome/Unity keyboard layout indicator.

gsettings set org.gnome.desktop.input-sources current 0

0 is the layout index (0 default or top layout). Layouts indexed starting from 0.

For easy use, create an alias.

6
  • 3
    This answer is superior because it actually changes the indicator. Just what I've been looking for.
    – Ainar-G
    Apr 27, 2017 at 20:50
  • 1
    thanks. used your answer also here: askubuntu.com/a/984981/6193
    – Alexey
    Dec 10, 2017 at 15:11
  • 4
    This doens't work on GNOME 3.26. It says "This key is deprecated and ignored".
    – hugomg
    Mar 3, 2018 at 14:21
  • @hugomg, right now I don't have access yet to newer gnome. but you may try using dconf-editor tool and check /org/gnome/desktop/. they may just rename it.
    – user.dz
    Mar 3, 2018 at 15:42
  • if you're using gnome.
    – RichieHH
    Oct 8, 2020 at 5:48
11
setxkbmap us,il -option "lv3:ralt_alt,grp:alt_shift_toggle"

this command enable you toggle between English and Hebrew only through by right alt+shift and rescue you from left alt+shift.

0
8

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.


Caveat

If your ~/.config/kxkbrc uses 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

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

6

Using gsettings in 18.04+ with Gnome, you can both 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:

    cat //usr/share/X11/xkb/rules/base.lst
    
  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']
...
2
  • This is the most comprehensive answer, kudos for this. Oct 14, 2018 at 10:45
  • This part doesn't seem to work on Ubuntu 23.04 with Wayland: gsettings set org.gnome.desktop.input-sources current 1
    – Flimm
    Jun 20, 2023 at 19:57
3

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[0].activate()"

The argument in "inputSources[0]" is the index of the keyboard layout, the same as for gsettings solution.

Found it on linux StackExchange.

3
  • Apparently, this doesn't work for GNOME >= 41. See the comments in that answer.
    – Flimm
    Jun 20, 2023 at 19:58
  • 1
    @Flimm For Gnome >= 41, eg Ubuntu 22, I have another answer
    – 1mi
    Jun 21, 2023 at 18:51
  • Thanks. Small nit-pick, there is to such thing as "Ubuntu 22", you either meant "Ubuntu 22.04" (released in April 2022) or "Ubuntu 22.10" (released in October 2022). The version numbers are confusing, but they refer to the year and month of the date of release.
    – Flimm
    Jun 21, 2023 at 19:41
1

I was able to find the best and most stable combination of xkb-switch, setxkbmap and ibus. IBus allows pinyin input and xkb-switch is stable, while setxkbmap returns back set of keyboard layouts.

This is important when switching directly to a language, for example Shift+Alt+1...4. Why not using only IBus? It gets stuck all the time because it requires a restart to bring keyboard layouts back to life after switching to non-latin keyboard layout. At least in FireFox, see I need to exit and restart ibus-daemon to make ibus-chewing work. #2319.

Disclaimer: Tested on OpenSuse Leap 15.3 xfce4 and xkb-switch is compiled from source

To make this work you should make script files with those commands and assign keyboard shortcuts to run them. Also you might want to disable Alt+Shift shortcut in keyboard switch settings to remove unexpected behavior.

So, here are my set of commands:

Shift+Alt+1 - switch to English:

killall ibus-daemon
setxkbmap us,ru,pt
xkb-switch -s us

Shift+Alt+2 - switch to Russian:

killall ibus-daemon
setxkbmap us,ru,pt
xkb-switch -s ru

Shift+Alt+3 - switch to Chinese Mandarin Pinyin:

ibus-daemon -d
ibus reset-config
gsettings set org.freedesktop.ibus.general preload-engines "['libpinyin']"
setxkbmap us,ru,pt
xkb-switch -s us
ibus engine libpinyin

Shift+Alt+4 - switch to Portugese:

killall ibus-daemon
setxkbmap us,ru,pt
xkb-switch -s pt

Also assign the following script to run on system autostart.

killall ibus-daemon
1

After reading all the other answers in to this question, you'll soon realize: it's pretty hard to achieve in Ubuntu, as none of the methods that you could Google works properly.

  • The setxkbmap doesn't play nice with the GNOME Shell.
  • The gsettings set org.gnome.desktop.input-sources current is deprecated and doesn't work anymore
  • The gdbus call … org.gnome.Shell.Eval … is also deprecated due to security implications.

That's why, I've made my own GNOME Shell Extension and I'm sharing it with the world: Shyriiwook (also available @ GitHub: madhead/shyriiwook).

This is a very simple, minimalist extension. It doesn't have any GUI. After installing it, a new D-Bus interface would be exposed in your GNOME Shell session. You could query it for the current configuration or call a method to activate the desired layout:

$ gdbus introspect \
    --session \
    --dest org.gnome.Shell \
    --object-path /me/madhead/Shyriiwook \
    --only-properties

node /me/madhead/Shyriiwook {
  interface me.madhead.Shyriiwook {
    properties:
      readonly as availableLayouts = ['us', 'de', 'jp'];
      readonly s currentLayout = 'us';
  };
};

$ gdbus call \
    --session \
    --dest org.gnome.Shell \
    --object-path /me/madhead/Shyriiwook \
    --method me.madhead.Shyriiwook.activate "de"

This is easily scriptable, and you can even put this command raw into a custom shortcut under the "Settings" → "Keyboard" → "Keyboard Shortcuts" → "View and Customise Shortcuts" → "Custom Shortcuts".

0

It may look hacky but it works like a charm:

setxkbmap -layout ru && setxkbmap -layout ru,us
setxkbmap -layout us && setxkbmap -layout us,ru

To force switch:

setxkbmap -layout ru && setxkbmap -layout us && setxkbmap -layout us,ru
0

Still valid with Ubuntu 22.04 in virtualbox 7.0:

Even when choosing the correct keyboard at install, you get a qwerty keyboard by default at the console. To get rid of it type this for eg Belgian:

loadkeys be

Then save the value to keep it permanent

vi /etc/default/keyboard
/XKBLAYOUT
/us
cwbe<ESC>
ZZ

as found in this post: How to permanently configure keyboard

-1

Just out of curiosity, why would you do that ? Why would you open a terminal and they type a command to switch your keyboard layout when you can do so by a simple keyboard shortcut ! Assuming you use Ubuntu , it goes as Windows + Space

4
  • 3
    This appears to be more of a comment and less of an answer. Feb 26, 2017 at 15:48
  • 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.
    – Eyal
    Mar 8, 2017 at 19:33
  • also it is useful for making custom keyboard-shortcuts
    – Vassilis
    Oct 13, 2017 at 11:24
  • This does not work in Ubuntu 16.04 on a virtualbox. Feb 15, 2018 at 9:48

You must log in to answer this question.

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