Skip to content

Instantly share code, notes, and snippets.

@honzakral
Created December 2, 2019 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save honzakral/4f6e293a5830253e653ac2c076567465 to your computer and use it in GitHub Desktop.
Save honzakral/4f6e293a5830253e653ac2c076567465 to your computer and use it in GitHub Desktop.
External display switcher
#!/bin/sh
PRIMARY=$(xrandr| grep -P '^[[:alnum:]-]+ connected primary' | cut -d ' ' -f 1)
SECONDARY=$(xrandr| grep -P '^[[:alnum:]-]+ connected (?!primary)' | head -n 1 | cut -d ' ' -f 1)
OUTPUTS=$(xrandr | sed -ne '2,$s;^\([^ ]\{1,\}\).*;\1;p' | grep -v "^$PRIMARY\$")
CMD="xrandr --output $PRIMARY --primary --auto "
function resolution {
xrandr | sed -ne "/$1/,/^[^ ]/s;^ [[:space:]]*\([0-9xip]\{1,\}\) .*;\1;p"
}
# turn off all unused outputs
for output in $OUTPUTS; do
if [[ "$output" != "$SECONDARY" ]]; then
CMD="$CMD--output $output --off "
fi
done
if [[ "$1" == "off" ]] || [[ -z "$1$SECONDARY" ]]; then
if [[ "$SECONDARY" ]]; then
# explicit ask to turn off secondary output
CMD="$CMD--output $SECONDARY --off"
fi
elif [[ "$SECONDARY" ]]; then
PS3="Select resolution: "
select RES in $(resolution $SECONDARY) auto abort
do
[[ "$RES" ]] || continue
[[ "$RES" == "abort" ]] && exit 1
# turn on secondary output
if [[ "$RES" == auto ]]; then
CMD="$CMD--output $SECONDARY --${1:-left-of} $PRIMARY --auto"
else
CMD="$CMD--output $SECONDARY --${1:-left-of} $PRIMARY --mode $RES"
fi
break
done
else
echo "No connected secondary!"
exit 1
fi
exec $CMD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment