How to set languages/shortcuts from config files or command line?

5,027

The keys

The keys you are looking for are:

  1. for editing the input sources:

    org.gnome.desktop.input-sources sources
    
  2. for editing the keyboard shortcut for switching languages forward:

    org.gnome.desktop.wm.keybindings switch-input-source
    
  3. for switching input source backward:

    org.gnome.desktop.wm.keybindings switch-input-source-backward
    

The commands to edit the settings

Example commands to edit the three from command line:

  1. For editing the input sources, to set the input sources as mentioned in your question:

    gsettings set org.gnome.desktop.input-sources sources "[('xkb', 'us'), ('xkb', 'it'), ('xkb', 'de')]"
    
  2. to set the switch- shortcut key combination to Ctrl+space

    gsettings set org.gnome.desktop.wm.keybindings switch-input-source "['<Primary>space']"
    
  3. to set the switch- shortcut key combination to switch backward Shift+Super+space

    gsettings set org.gnome.desktop.wm.keybindings switch-input-source-backward "['<Shift><Super>space']"
    

The commands to get the current settings

gsettings get org.gnome.desktop.input-sources sources
gsettings get org.gnome.desktop.wm.keybindings switch-input-source
gsettings get org.gnome.desktop.wm.keybindings switch-input-source-backward

How to find these keys

You can search for keywords in the database by listing all keys with the command:

gsettings list-recursively

which will list all existing keys, or (which I regularly do) install dconf-editor and simply find your keyword(s) with Ctrl+F.


Edit

As requested in a comment, I added a small script to walk through the input sources from command line:

#!/usr/bin/env python3
import subprocess
import sys

src = sys.argv[1]
key = "org.gnome.desktop.input-sources"
get = lambda cmd: subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8").strip()

src_list = len(eval(get("gsettings get "+key+" sources")))-1
current = int(get("gsettings get "+key+" current").split()[-1])
if src == "+":
    set_new_value = "gsettings set "+key+" current "+(str(current+1) if current <  src_list else "0")
elif src == "-":
    set_new_value = "gsettings set "+key+" current "+(str(current-1) if current > 0 else str(src_list))
subprocess.Popen(["/bin/bash","-c", set_new_value])

To run it

  1. Save the script, into an empty file, as switch_source.py
  2. Run it by the command:

    python3 /path/to/switch_source.py +
    

    to go to the next input source, and

    python3 /path/to/switch_source.py -
    

    to go to the previous one

Share:
5,027

Related videos on Youtube

user779159
Author by

user779159

Updated on September 18, 2022

Comments

  • user779159
    user779159 over 1 year

    I need to provision several Ubuntu 14.04/14.10 desktops. Most things I've been able to provision easily because they can be set in config files or from the command line.

    But I'm having trouble with 2 things, setting the available languages and setting the keyboard shortcuts for switching between the languages. I know how to do them from gnome-control-center

    1. Set the available languages

      Region & Language > Input Sources > English (US) + German + Italian

    2. Set the keyboard shortcuts for switching languages

      Shortcut Settings > Typing > Switch to next source (Super+Escape), Switch to previous source (Shift+Super+Escape)

    But I don't know how to do from config files or command line, so I can't provision it automatically.

  • user779159
    user779159 about 9 years
    I will test this out and report back. 'switch-input-source' would be for 'Switch to next source', how can I find the key for 'Switch to previous source'?
  • user779159
    user779159 about 9 years
    How did you find these keys btw, is there a simple way to tell which keys correspond to which settings in gnome-control-center? Like is it documented somewhere or is there a technical way to find out?
  • Jacob Vlijm
    Jacob Vlijm about 9 years
    @user779159 edited my posts with answers to both comments :)
  • user779159
    user779159 about 9 years
    Setting the sources works great, thanks! But switching isn't working. From the command line doing setxkbmap de works fine, but not with keyboard shortcuts, nothing happens. Looking through the settings I see org.gnome.settings-daemon.peripherals.keyboard input-sources-switcher 'off', could it be related?
  • Jacob Vlijm
    Jacob Vlijm about 9 years
    @user779159 "off" is the default setting for that key, but switching should work still. Silly question, but what is the output of gsettings get org.gnome.desktop.wm.keybindings switch-input-source? Another cause could be that the key combination you set is not valid or not available.
  • user779159
    user779159 about 9 years
    I reset the switch-input-source value to its default value but it still doesn't work. Do you know what command these keyboard shortcuts call under the hood to switch the language forwards and backwards through the list of languages set in org.gnome.desktop.input-sources sources? If you wanted to call that same command from the command line do you have any idea what it would be or how I could call it?
  • user779159
    user779159 about 9 years
    Thanks for all your help with this, marked answer accepted. Btw Ubuntu must have some equivalent of that script you wrote right? Because Ubuntu is running something when you do the switch-input-source shortcut, so there should be a script somewhere that would do this. Any clues as to how to find it?
  • Fabby
    Fabby about 9 years
    And another one gets bitten by the python! ;-)
  • Jacob Vlijm
    Jacob Vlijm about 9 years
    @user779159 The fact that Ubuntu runs something does not mean that we can run the same thing in bash or any other language, like you cannot run a command for every key combination in many applications. It depends on the command line options of an application.
  • Jacob Vlijm
    Jacob Vlijm about 9 years
    @Fabby I just need an excuse to pythonize something now and then :).