How can I toggle between xkb keyboard layouts in Sway from the command line? My layouts are configured like this:

input type:keyboard {
    xkb_layout pl,es
    xkb_options grp:win_space_toggle,compose:caps

I'd like to be able to have a command that switches to the next layout (to pl if the current one is es, to es if the current one is pl).

Note: There is a command swaymsg "input type:keyboard xkb_switch_layout <index>" that allows you to change the layout specifying it's index. I don't know how to use that to toggle to the next one, as I don't know how to get the current index.

3 Answers 3


Using sway version 1.5.1, I can provide next to xkb_switch_layout. So you no longer need to get the current index and number of total items.

swaymsg input "1:1:AT_Translated_Set_2_keyboard" xkb_switch_layout next

Unfortunately, I cannot use type:keyboard instead of the identifier. You can find your identifier with swaymsg -t get_inputs. If you want to see all available layouts and the index of the current layout you would need to add --raw.

  • 1
    i have sway 1.8.1 on Fedora 38 and can confirm type:keyboard works instead identifed
    – scorpp
    May 28, 2023 at 12:27

You can retrieve the current keyboard layout index of your input by running swaymsg -t get_inputs, which will give you a JSON containing an array of inputs and their associated settings.

The keys you are looking for are xkb_active_layout_index and xkb_layout_names.

Here is an example of the output given by the command on my system (cropped to show the relevant part):

    "identifier": "16700:8467:Dell_KB216_Wired_Keyboard",
    "name": "Dell KB216 Wired Keyboard",
    "vendor": 16700,
    "product": 8467,
    "type": "keyboard",
    "xkb_layout_names": [
      "English (US)",
      "Romanian (standard)"
    "xkb_active_layout_index": 0,
    "xkb_active_layout_name": "English (US)",
    "libinput": {
      "send_events": "enabled"

To simulate the behavior of sway's toggle, you can use the following code:


inputdata=$(swaymsg -t get_inputs | jq ".[] | select(.identifier==\"$inputid\")")
index=$(echo "$inputdata" | jq ".xkb_active_layout_index")
layoutcount=$(echo "$inputdata" | jq ".xkb_layout_names | length")
swaymsg input "$inputid" xkb_switch_layout $((($index + 1) % $layoutcount))

Using sway version 1.4


You already have the correct configuration. Super+Space should change your keyboard layout (that is what win_space_toggle means). There are other key configurations for switching keyboard layouts, you can find them in the man pages: man 7 xkeyboard-config.

  • Yes, Win+Space key combination will switch the layout. But I was asking for a command line solution - a command or Bash script that I could execute and would change the layout. Apr 2, 2020 at 16:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .