I3wm/gxkb usage with multiple keyboard layouts question

Hello. I’ve been using i3wm flavor of Endeavour OS for a couple months now on my laptop and I love it, however there are some troubles I have with a few things.

So this one is about keyboard layout switching in i3wm. I use gxkb for this to have a kb layout switcher and a tray icon for it, and currently I have 2 keyboard layouts with switching them by alt-shift. If i want to use a third layout, every time I need to switch language I type in it would turn into a guessing game or a need to look at indicator to make sure I switched to the correct language, which is inefficient and time-wasting. Hence the question: is it possible to have, say, two groups of layouts, with different switching keybindings for them? As in “use alt-shift to switch between 1st & 2nd layouts and some other keys to switch between 1st & 3rd layouts”?

I also use Garuda Linux KDE (with Bismuth for tiling windows) on my desktop computer, and in KDE there’s an option called “spare layout” where you can add an additional keyboard layout that won’t be included in regular cycling of the layouts switcher and set a different keyboard shortcut for switching to that layout specifically, is there any way to achieve the same effect in i3wm? I tried looking into gxkb and setxkbmap manuals and discussions, but couldn’t find or figure out how to do it :c

You can add a third layout also and toggle between 1, 2 and 3 with your regular keyboard shortcut Alt+Shift.

68747470733a2f2f7a656e2d746f6f6c732e6769746875622e696f2f67786b622f696d616765732f67786b625f747261795f6c61796f7574732e706e67

Configuration
Configuration is done via config file: .config/gxkb/gxkb.cfg
The most interesting options are:
layouts=us,ru,ua
toggle_option=grp:alt_shift_toggle,grp_led:scroll,terminate:ctrl_alt_bksp

Instead of grp:alt_shift_toggle you can use whatever the following command gives you: grep grp:.*toggle /usr/share/X11/xkb/rules/base.lst

I’ve read that, thanks, but:

  1. I don’t want to “switch between 1, 2 and 3 with one shortcut”, I want to have one shortcut for “switching between 1 and 2” and another shortcut for “switching between 1 and 3”,
    or [a shortcut for “switching between 1 and 2” + “a shortcut for switching to keyboard layout 3”], like how it works in KDE, whichever is achievable in WMs

  2. I haven’t found there information if having “2 groups of 2 layouts with different switching shortcuts” is actually possible or how to do it.
    Also, when searching about what gxkb config file does, surprisingly, very little information comes up searching for what do “group_policy=2” and
    default_group=0” mean in xkb config, so if anyone knows what those do or can point me to any manuals describing what those do, I’ll appreciate that too

My bad, turns out I’ve read it not as thoroughly as I remembered.

  1. group_policy=2 meaning is described here:
    https://github.com/zen-tools/gxkb/issues/22#issuecomment-613316444

  2. After starting this forum topic I tried searching again and found next things:

https://forums.linuxmint.com/viewtopic.php?t=286032
-interesting, almost it

-mostly really it

Next, I first tried the solution from Linux Mint forum’s link: I installed xkb-switch-i3 from AUR (regular xkb-switch package works too), then tried doing the scripts posted here. Didn’t work, because it’s a distinctly different enough kind of problem they’re solving there, but I learned a bit about xkb-switch package.

Then, I read the second link. That was it! Quite a similar description of problem too, so i tried it. In the end I cobbled together this solution with some xkb-switch commands.

What do I have as the end result:
First, what I was trying to achieve:
In KDE, I use 3 layouts: [us, ru, ua]. The way the “spare layout” feature works there, as I understand it in the case of having 3 layouts, is something like this:

  • I have layouts [us, ru] as main set, switching is done by alt-shift
  • to switch to layout [ua] (spare) you set a different shortcut
  • after pressing that shortcut, layouts 2 and 3 switch places, so in my case the main set instead of [us, ru] becomes [us, ua], now [ru] is considered “spare” for the next use of “spare layout shortcut”, so now alt-shift switches between [us, ua]
  • out of any position in [us, ru] set, the “spare switching shortcut” switches to [ua]
  • out of any position in [us, ua] set, the “spare switching shortcut” switches to [ru]

What I ended up with:
Here’s how I edited the script that I found in SergeM’s github post:

#!/bin/bash
current=`setxkbmap -query | grep layout|cut -d ',' -f 2`
echo $current
if [ "$current" == "ua" ]
then 
    notify-send -t 750 -i keyboard "EN/RU"
    setxkbmap -model pc105 -layout us,ru, -option grp:alt_shift_toggle
    xkb-switch -s ru
else 
    notify-send -t 750 -i keyboard "EN/UA"
    setxkbmap -model pc105 -layout us,ua, -option grp:alt_shift_toggle
    xkb-switch -s ua
fi

Notes: I added xkb-switch commands to the if statements, so they’ll result in expected behavior, current=xkb-switch or current=xkb-switch -p seems faster, but I accidentally broke all my WM’s keyboard shortcuts spamming this version of script for testing, and had to reboot so I’m not sure about that :rofl:
I made a shortcut mod+shift+u in my i3 config file to execute this script and now I have on my i3wm setup this behavior: main keyboard switching shortcut is alt-shift, original set is [us, ru] if the current layout is [us] or [ru], mod+shift+u replaces [us, ru] with [us, ua] and switches to [ua], if the current layout is [ua], mod+shift+u replaces [us, ua] with [us, ru] and switches to [ru]. Which is like 95% what I wanted (only one case differs from what happens in KDE setup).

Possible ways to improve the solution:

  • maybe using better logic and adding nested if statements into this bash script can recreate this behavior I described in KDE
  • out of any position in [us, ru] set, the “spare switching shortcut” switches to [ua]
  • out of any position in [us, ua] set, the “spare switching shortcut” switches to [ru]

Currently, if the group [us, ua] is active in [us] layout, the mod+shift+u will switch to [ua], because that’s exactly what the end result script says. Since I don’t know how to make it better, the current “95% it” version will do.
I hope this was helpful or interesting

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.