Skip to content

Instantly share code, notes, and snippets.

@bynect
Created June 5, 2024 11:33
Show Gist options
  • Save bynect/868586a28988675c305111d21c606a1c to your computer and use it in GitHub Desktop.
Save bynect/868586a28988675c305111d21c606a1c to your computer and use it in GitHub Desktop.
Xrandr multi screen auto setup
#!/bin/bash
#
# Layout and Resolution
# NOTE: Equivalent settings set in xorg.conf
# NOTE: Never mind, xorg.conf is way too convoluted
SCREEN_NO="$(xrandr --query | grep '\bconnected\b' | wc -l)"
echo "Setting up $SCREEN_NO screen(s) with xrandr"
prev_width=0
prev_height=0
width_off=0
height_off=0
while read screen width height rate primary
do
echo "Screen ${primary:-secondary}: $screen ${width}x${height}"
[[ "$prev_width" == 0 ]] || width_off=$((width_off - width))
[[ "$prev_height" == 0 ]] || height_off=$(((height - prev_height) / 2))
xrandr --output "$screen" \
--mode "${width}x${height}" \
--pos "${width_off}x${height_off}" \
--rate "$rate" \
$([ "$primary" = "primary" ] && echo "--primary")
prev_width=$width
prev_height=$height
done <<< \
"$(xrandr --query | perl -0ne 'while (/^(\w+-\d+) connected (primary )?.*\n\s+(\d+)x(\d+)\s+([\d\.]+)(?:\s\+\s([\d\.]+))?.*$/gm) { if ($6 eq "") { $r=$5; } else { $r=$6; } print "$1 $3 $4 $r $2\n"; }')"
# Update background
feh --no-fehbg --bg-scale $HOME/img/background
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment