Skip to content

Instantly share code, notes, and snippets.

@lmas
Created November 1, 2023 21:55
Show Gist options
  • Save lmas/14abd69cac40b5fb012a4bdcd24fca44 to your computer and use it in GitHub Desktop.
Save lmas/14abd69cac40b5fb012a4bdcd24fca44 to your computer and use it in GitHub Desktop.
Switch between monitors using xrandr, while running bspwm
#!/bin/sh
# This script tries to automatically switch to a plugged in monitor
# and handles any quirks or issues while doing so.
# Stores current desktop (bspwm switches to some other empty desktop after switch)
desk=$(bspc wm -d | jq ".monitors[].focusedDesktopId")
# Switch active monitor
if xrandr -q | grep -q "HDMI-2 connected"; then
xrandr --output eDP-1 --off --output HDMI-2 --primary --auto
else
xrandr --output eDP-1 --primary --auto --output HDMI-2 --off
fi
# Change back to "current" desktop
if [ -n "$desk" ]; then
bspc desktop -f "$desk"
fi
# Get new screen rectangle for the new monitor and notify bspwm about it
rect=$(xrandr -q | grep -oe "[0-9][0-9][0-9][0-9]*x[0-9][0-9][0-9][0-9]*+0+0")
if [ -n "$rect" ]; then
bspc monitor -g "$rect"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment