Ubuntu – Assign default keyboard language per-application


Is it possible to assign default keyboard language for specific applications? I know that there is an option to always start an application with a default keyboard language but this is not what I want.

I'm looking for something similar to "fixed workspaces" which you can set using CompizConfig (You set Chrome to always open at X1Y1, terminal at X2Y1 etc). Something where I would set Chrome: Czech, terminal: English, Spotify: English, …

Best Answer


The script bellow sets the language for each user-defined program, according to the position of that language in the language menu. For instance, if my order is: English (1), Chinese(2), and Russian(3), I can set Firefox to have language 2, terminal to have language 1, and LibreOffice to have language 3.

The script comes in two parts: part one is the actual script that does the job, the second script serves as a controlling element. The idea is to run the language setting script as a start-up application and whenever you need to manually change language - double click the shortcut to the controller script.


  1. Install wmctrl program with sudo apt-get install wmctrl.


# Author: Serg Kolo
# Date: August 4, 2015
# Description: This script forces assigned input languages
#              for specific windows
# Version:2

# Use this part to set programs and their respective languages
# PROG_CLASS or a running window can be found with the
# wmctrl -lx command
# If you want to add another program to the list, 
# follow the template PROG_CLASS_num=window.Class
# and bellow that $LANGnum=num




# While loop below gets the job done. 
# If you need to send languages for more programs -  copy  
# the first entry and replace  $PROG_CLASS_1 with $PROG_CLASS_num
# where num is respective number of a program
# Replace $LANGnum with the respective language number. After the "="
# post positional number of the language you want to use. 
# Remember the count starts from 0

while [ 1 ];do
  WM_CLASS=$(wmctrl -lx | awk -v search=$(printf %x $(xdotool getactivewindow)) '{ if($1~search) print $3 }' )
  CURRENT=$(gsettings get org.gnome.desktop.input-sources current| awk '{print $2}')
    case  $WM_CLASS in

        if [ $CURRENT -ne  $LANG1 ];then
          gsettings set org.gnome.desktop.input-sources current $LANG1

        if [ $CURRENT -ne  $LANG2 ];then
          gsettings set org.gnome.desktop.input-sources current $LANG2

         if [ $CURRENT -ne  $LANG3 ];then
           gsettings set org.gnome.desktop.input-sources current $LANG3

    sleep 0.250


Controller Script

# set -x
# Author: Serg Kolo
# Date: August 8, 2015
# Description: Controller script for set-lang.sh script
# Allows pausing and resuming execution of set-lang.sh
STATUS=$(ps -o stat -p $(pgrep -o  set-lang.sh) | awk '{getline;print }')

case $STATUS in
    T) kill -CONT $(pgrep set-lang.sh) 
       notify-send 'RESUMED'

    S) kill -STOP $(pgrep set-lang.sh)
       notify-send 'STOPED'

    *) exit ;;

Launcher (.desktop) file for set-lang.sh script

[Desktop Entry]
Comment=Script to set languages

Launcher (.desktop) file for set-lang-controller.sh

[Desktop Entry]
Comment=Shortcut to controlling script

Making the script work

  1. Create a folder in your home directory called bin. You can do it in the file manager or use the command mkdir $HOME/bin in the terminal
  2. In the bin folder create two files: set-lang.sh and set-lang-control.sh. Save script to set-lang.sh and controller script to set-lang-control.sh. Make both scripts executable with sudo chmod +x $HOME/bin/set-lang-control.sh $HOME/bin/set-lang.sh
  3. Create two .desktop files. One is set-lang.desktop. Must be placed in the hidden .config/autostart directory. Second one is set-lang-controller.desktop, may be placed in your bin folder. Next drag and pin to the launcher the set-lang-controller.desktop file. This will become the shortcut for temporarily stopping and resuming the script execution.

NOTE that the line Exec= must be altered to have your actual user name in the path to script (because that's your actual home directory). For example, mine would be Exec=/home/serg/bin/set-lang.sh

Explanation and customization:

The script itself runs in an infinite while loop and checks the current active window. If the current active window matches one of the options in the case structure, we switch to the appropriate language. To avoid constant setting, each part of the case structure has if statement that checks if the language has already been set to the desired value.

The double clicking on the launcher for set-lang-controller.sh will check the status of the set-lang.sh script; if the script is running - it will be paused, and if the script is paused it will be resumed. A notification will be shown with appropriate message.

In order to customize the script, you can open desired application(s), run wmctrl -lx and note the third column - the window class. Sample output:

$ wmctrl -lx | awk '$4="***" {print}'                                                                                                            
0x02c00007 0 gnome-terminal-server.Gnome-terminal *** Terminal
0x03a0000a 0 desktop_window.Nautilus *** Desktop
0x04a00002 0 N/A *** XdndCollectionWindowImp
0x04a00005 0 N/A *** unity-launcher
0x04a00008 0 N/A *** unity-panel
0x04a0000b 0 N/A *** unity-dash
0x04a0000c 0 N/A *** Hud
0x012000a6 0 Navigator.Firefox *** unity - Assign default keyboard language per-application - Ask Ubuntu - Mozilla Firefox

Select the appropriate window classes for each program. Next, go to the part of the script that allows customization and add two entries for PROG_CLASS and LANG. Next add the appropriate entry in the case structure.

For instance, if I want to add, LibreOffice's Writer, I open LibreOffice Writer window, go to terminal and run wmctrl -lx. It will tell me that the Writer window has class libreoffice.libreoffice-writer. Next I will go to the script, add PROG_CLASS_4=libreoffice.libreoffice-writer and LANG4=3 in the appropriate area. Notice matching number 4. Next, go to case structure, and add the following entry between last ;; and esac :

  if [ $CURRENT -ne  $LANG4 ];then
    gsettings set org.gnome.desktop.input-sources current $LANG4

Again, notice the $ sign and matching number 4.

Also, if the script is running as an autostart item and you want to stop it temporarily to customize it, use pkill set-lang.sh and resume with nohup set-lang.sh > /dev/null 2&>1 &

Small note: another way to find out the window class for a program (that stuff that goes before single round bracket in the case structure) is to use this xprop and awk oneliner : xprop | awk '/WM_CLASS/ {gsub(/"/," "); print $3"."$5}