To configure several keyboard input layouts, it’s quite simple - especially if you come from i3: no need for ibus or fcitx (both bring their own problems in Sway/Wayland).
All you have to do is edit your keyboard input preferences right in your Sway config file. For example:
input type:keyboard {
xkb_layout us,fr,fi,es
xkb_options grp:alt_space_toggle,lv3:ralt_switch
}
This sets up 4 keyboard layout (US, French, Finnish and Spanish) and Alt+space
as the shortcut to cycle through them.
There’s a plethora of XKB layouts and options. You can see them all by doing man xkeyboard-config
. Pick the ones you want.
Then if you use Waybar, you can set it up so that it displays the layout currently in use with the sway/language
module. For instance, put this in your Waybar config file:
"sway/language": {
"format": "{flag}",
"tooltip": false,
"on-click": "swaymsg input type:keyboard xkb_switch_layout next",
"on-click-right": "swaymsg input type:keyboard xkb_switch_layout prev"
},
This shows the current layout as the corresponding country flag and configures left- and right-click to cycle through the layouts when clicking on the flag.
Finally, if you like to type emojis and you don’t want to remember the unicode numbers (which you can enter in hexadecimal with Ctrl+Shift+u
in case you didn’t know), install rofimoji and wtype
: rofimoji
is a very nice Rofi-based emoji picker, and wtype
is the Wayland typer it needs to inject the emojis through the Wayland virtual keyboard:
Then add a key binding in your Sway config file to call the emoji picker. I personally use Ctrl+Shift+e
:
bindsym Ctrl+Shift+e exec rofimoji
Final twist: if you use several input layouts and you display the current layout in Waybar, you’ll notice that wtype
(called by rofimoji
) makes the Waybar language icon disappear.
That’s because there’s a bug in the Waybar Sway language module: it doesn’t mess up the keyboard input, it just makes the icon disappear until you change the layout, and then it comes back.
So here’s a workaroud, since nobody seems to be in a hurry to fix this bug 😃:
- In
.local/bin
, create awtype
file with the following content:
#!/bin/sh
/usr/bin/wtype $@ && swaymsg input type:keyboard xkb_switch_layout next && swaymsg input type:keyboard xkb_switch_layout prev
What this script does is call the real wtype
, then switch the keyboard layout back and forth to make the Waybar language icon reappear.
-
Add
.local/bin
to your PATH environment variable BEFORE/usr/bin
, so yourwtype
wrapper is called byrofimoji
in lieu of the realwtype
. To do that in Sway:- Add the modified PATH to ~/.config, e.g.:
PATH=$HOME/.local/bin:$PATH
- call
systemctl --user daemon-reload
- Log out and back in
(if you wonder why you have to do all this to set a simple environment variable in Sway, see here)
After that, the language icon should stay put when you enter emojis.