#!/bin/bash _self=$(basename "$0") pgrep -f "$_self" | grep -v $$ | xargs kill -9 2>/dev/null declare -A layouts prev_win_id="" while read -r event; do if [[ "$event" == *'"change":"focus"'* ]]; then current_win_id=$(echo "$event" | grep -oP '"id":\K[0-9]+' | head -n 1) if [ -n "$current_win_id" ]; then if [ -n "$prev_win_id" ] && [ "$prev_win_id" != "$current_win_id" ]; then layouts[$prev_win_id]=$(xkb-switch -p) fi saved_layout=${layouts[$current_win_id]} if [ -n "$saved_layout" ]; then xkb-switch -s "$saved_layout" else layouts[$current_win_id]=$(xkb-switch -p) fi prev_win_id=$current_win_id fi fi done < <(stdbuf -oL i3-msg -t subscribe -m '[ "window" ]')