Switch keyboard layouts with i3

14,178

Solution 1

You can do it with xkblayout-state and i3blocks

For xkblayout-state:

I assume you have git.

~$ git clone https://github.com/nonpop/xkblayout-state.git
~$ cd xkblayout-state
~$ make

Now copy compiled xkblayout-state somewhere in your PATH, for example:

~$ `cp xkblayout-state /usr/local/bin/`

Now for i3blocks:

sudo apt-get install i3blocks
cp /etc/i3blocks.conf ~/.i3blocks.conf

In your i3 config file (~/.config/i3/config) replace i3status with i3blocks,

# Start i3bar to display a workspace bar (plus the system information i3status
# finds out, if available)
bar {
        status_command i3blocks
        tray_output primary                                               
}
# in case of layout change event - send signal to i3blocks
bindsym ISO_Next_Group exec pkill -RTMIN+1 i3blocks

And restart i3wm (mod+shift+R), you should now see that i3blocks appeared.

Open your ~/.i3blocks.conf and add somewhere (for example, right above [VOLUME] section):

#Language indicator
[language]
#label=LNG
command=xkblayout-state print %s | awk '{print toupper($0)}'
interval=once
signal=1

Again, restart i3wm (mod+shift+R), and you should see your locales in status bar, switching.

Check out xkblayout-state --help, there is many options for different output.

Solution 2

Follow up to @aring's answer:

You can do it with xkblayout-state and i3blocks

Switching back and forth between two languages with xkblayout-state, put this in your ~/.config/i3/config file:

bindsym $mod+b exec xkblayout-state set $(( $(xkblayout-state print %c) ^ 1 ))
Share:
14,178

Related videos on Youtube

Niakrais
Author by

Niakrais

Updated on September 18, 2022

Comments

  • Niakrais
    Niakrais over 1 year

    I have a fresh i3 version 4.11 (2015-09-30, branch "4.11") installed over my Ubuntu 16.04. Actually I need to use 3 different keyboard layouts for example en, fr and ru with the possibility to switch between them by using some keyboard shortcut.

    I have tried some methods but neither was completely satisfactory:

    • Using ibus-daemon almost works but I cannot use any shortcuts for change layouts.
    • My actual setup is configuring setxkbmap when I start i3:

      exec --no-startup-id "setxkbmap -model pc105 -layout fr,us,ru -option grp:alt_shift_toggle"
      

    So I can switch between my layouts but sometimes I have no idea which layout I am actually in use because there is not any tray icon for that in this setup.

    For conclusion my needs are:

    • Be able to switch layouts by pressing some hotkeys.

    • Some indications(preferably, tray icon) for layout I am actually in use.

    Is there some work around about this? Or maybe there is some i3 plugin tha t actually does the job? Some third software?

    • Ingo Bürk
      Ingo Bürk over 7 years
      This is not a window manager task, so don't look in i3 for that. Please do some research, this has been asked and answered before. Anyway, regarding what @shellter said, i3 has a subreddit (i3wm) for these kind of questions.
    • Niakrais
      Niakrais over 7 years
      @IngoBürk all questions are answered somewhere:) I agree that it's not a window manager task but it's surely a job for desktop environment! So I am looking for some tool(s)/solution that will do what i want in combo with i3. Anyway thank you for subreddit advice, I'll look for that.
  • Niakrais
    Niakrais over 7 years
    So the key is using i3blocks in place of i3status.
  • aring
    aring over 7 years
    @Niakrais, the key is get shit done. There is many ways, and this is one of them. Current problem - i3status is not so flexible (that's why more sane i3blocks) and there is no easy way to get current keyboard layout, so - that's why there xkblayout-state.Bonuses of such approach: clear, concise. And you can easily change/expand i3blocks later. xkblayout-state can be easily used somewhere else too. If you want to stick with i3status - you can look here also: reddit.com/r/i3wm/comments/4ib2fp/…
  • Niakrais
    Niakrais over 7 years
    Sure and it works fine, I ever tried to set a interval to -2 for xkblayout-state's check but It overheats my processor:)
  • aring
    aring over 7 years
    @Niakrais, yes, thanks to current multi-core technologies and gigs of ram - we can shoot in our leg faster than ever. If you need to monitor your kbd layout every millisecond - you can do it, but it will be costly.
  • aring
    aring over 7 years
    @Niakrais, thanks to you, I started digging again - check edited answer now, it's way more better.
  • Niakrais
    Niakrais over 7 years
    thanks, I had in mind to test iblocks signals tomorrow:)
  • Gryu
    Gryu about 4 years
    @uchaosis I think, you could post your edit as an answer, quoting existing answer.
  • Mats Faugli
    Mats Faugli about 4 years
    I edited my answer just now to include the quote from @aring's answer.