Skip to content

Instantly share code, notes, and snippets.

@tmfink
Created April 11, 2015 20:16
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 tmfink/13ecf5f752f1c1f3f9ac to your computer and use it in GitHub Desktop.
Save tmfink/13ecf5f752f1c1f3f9ac to your computer and use it in GitHub Desktop.
Uses xrandr to detect and setup active outputs
#!/bin/bash
#
# Uses xrandr to detect and setup active outputs
##########
# Set to name of main display
MAIN="eDP1"
##########
ACTIVE_OUTPUTS=$(xrandr | grep -oE '^.+ connected' | cut -d" " -f1)
INACTIVE_OUTPUTS=$(xrandr | grep -oE '^.+ disconnected' | cut -d" " -f1)
NUM_ACTIVE_OUTPUTS=$(echo "$ACTIVE_OUTPUTS" | wc -l)
echo "Found $NUM_ACTIVE_OUTPUTS active outputs"
echo "Active: $(echo $ACTIVE_OUTPUTS | tr '\n' ' ')"
echo "Inactive: $(echo $INACTIVE_OUTPUTS | tr '\n' ' ')"
INACTIVE_OUTPUT_CMD=$(for X in $INACTIVE_OUTPUTS; do echo --output $X --off; done)
case $NUM_ACTIVE_OUTPUTS in
0)
echo "Error: found no active outputs"
exit 1
;;
1)
echo "Found one active output"
OUTPUT="$ACTIVE_OUTPUTS"
xrandr $INACTIVE_OUTPUT_CMD --output $OUTPUT --auto --primary
;;
2)
echo "Found two active outputs"
EXTERNAL=$(echo "$ACTIVE_OUTPUTS" | grep -v "^${MAIN}$")
echo "external is $EXTERNAL"
xrandr $INACTIVE_OUTPUT_CMD --output "$MAIN" --auto --primary \
--output "$EXTERNAL" --right-of "$MAIN" --auto
;;
*)
echo "Unsupported number of active outputs: $NUM_ACTIVE_OUTPUTS"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment