Skip to content

Instantly share code, notes, and snippets.

@davidfraser
Created November 22, 2012 14:10
Show Gist options
  • Save davidfraser/4131369 to your computer and use it in GitHub Desktop.
Save davidfraser/4131369 to your computer and use it in GitHub Desktop.
Script for switching laptop displays (see http://westmarch.sjsoft.com/2012/11/laptop-display-switcher/)
#!/bin/bash
target_displays="$1"
VGA=VGA-0
LVDS=LVDS-0
if [[ "$target_displays" == "auto" || "$target_displays" == "" ]]
then
xrandr_output="`xrandr -q`"
vga_connected=`echo "$xrandr_output" | grep "^$VGA " | cut -d' ' -f2`
lvds_connected=`echo "$xrandr_output" | grep "^$LVDS " | cut -d' ' -f2`
target_displays="external"
[ "$vga_connected" != "connected" ] && target_displays="laptop"
echo auto-selected target display "$target_displays"
elif [[ "$target_displays" == "toggle" ]]; then
xrandr_output="`xrandr -q`"
vga_connected=`echo "$xrandr_output" | grep "^$VGA " | cut -d' ' -f2`
lvds_connected=`echo "$xrandr_output" | grep "^$LVDS " | cut -d' ' -f2`
if [ "$vga_connected" != "connected" ];
then
target_displays="laptop"
echo vga not connected - displaying on $target_displays
else
vga_settings=`echo "$xrandr_output" | grep "^$VGA " | sed 's/(.*$//' | cut -d' ' -f3`
lvds_settings=`echo "$xrandr_output" | grep "^$LVDS " | sed 's/(.*$//' | cut -d' ' -f3`
# cycle through laptop, external, laptop+external
[ "$vga_settings" == "" ] && [ "$lvds_settings" != "" ] && target_displays=external # when laptop only
[ "$vga_settings" != "" ] && [ "$lvds_settings" == "" ] && target_displays=both # when external only
[ "$vga_settings" != "" ] && [ "$lvds_settings" != "" ] && target_displays=laptop # when both on
echo auto-selected target display "$target_displays"
fi
fi
if [[ "$target_displays" == "laptop" ]]
then
echo turning VGA off
xrandr --output $VGA --off
echo turning LVDS on auto
xrandr --output $LVDS --auto
elif [[ "$target_displays" == "2" || "$target_displays" == "both" ]]; then
echo turning VGA on to the right of LVDS
xrandr --output $VGA --right-of $LVDS --auto
echo turning LVDS on to the left of LVDS
xrandr --output $LVDS --left-of $VGA --auto
elif [[ "$target_displays" == "external" ]]; then
echo turning LVDS off
xrandr --output $LVDS --off
echo turning VGA on auto
xrandr --output $VGA --auto
else
echo unknown option "$@"
exit 1
fi
xrandr_output="`xrandr -q`"
vga_settings=`echo "$xrandr_output" | grep "^$VGA connected" | sed 's/(.*$//' | cut -d' ' -f3`
lvds_settings=`echo "$xrandr_output" | grep "^$LVDS connected" | sed 's/(.*$//' | cut -d' ' -f3`
vga_size=${vga_settings%%+*}
lvds_size=${lvds_settings%%+*}
vga_offset=${vga_settings#*x*+}
lvds_offset=${lvds_settings#*x*+}
set | egrep '^(vga|lvds)'
echo done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment