Skip to content

Instantly share code, notes, and snippets.

@dr0bz
Last active September 21, 2025 05:25
Show Gist options
  • Save dr0bz/4f8bf8b8829506cf26d4e2acc770e5af to your computer and use it in GitHub Desktop.
Save dr0bz/4f8bf8b8829506cf26d4e2acc770e5af to your computer and use it in GitHub Desktop.
Hyprland waybar switch by monitor hotpluging
#!/bin/bash
#
# This script is for people like me running a laptop with external monitor and using the
# external monitor as the main output. As main output I want to have the waybar show up
# on it. With this script you get waybar on external monitor and when you unplug it - it
# will automatically change the display to Your display and vice versa.
#
# just put the following into your hyprland.conf:
# exec-once = waybar
# exec-once = ~/.config/hypr/scripts/waybar-monitor-switch.sh
# stop running it twice
scriptname=$(basename $0)
running_count=$(ps aux | grep -i "${scriptname}" | grep -v "grep" | wc -l)
if [ $running_count -gt 2 ]; then
echo "Exiting: Already running..."
exit
fi
logger "${scriptname} started"
function handle {
case $1 in
monitoraddedv2*)
external_output=$(hyprctl monitors | awk '/Monitor DP-/ {print $2 }')
sed -i "s/\"output.*/\"output\": \[\"${external_output}\"\],/" ~/.config/waybar/config
sleep 1
killall -SIGUSR2 waybar
;;
monitorremoved*)
laptop_output='"eDP-1"'
sed -i "s/\"output.*/\"output\": \[${laptop_output}\],/" ~/.config/waybar/config
sleep 1
killall -SIGUSR2 waybar
;;
esac
}
socat -U - UNIX-CONNECT:$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock | while read -r line; do handle "$line"; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment