With udev you can trigger actions when an external screen/monitor is connected. This is, e.g., useful when you want to automatically perform some actions when an external Screen is connected to a laptop. Such actions could be to automatically enable the additional screen and set its orientation using xrandr or the like.
The following udev rule (e.g., in a file such as /etc/udev/rules.d/95-monitor-hotplug.rules) executes a script when the external VGA output is connected (Thanks to Andy for the hint in his post.):
#Rule for executing commands when an external screen is plugged in. #Credits go to: http://unix.stackexchange.com/questions/4489/a-tool-for-automatically-applying-randr-configuration-when-external-display-is-p ACTION=="change", SUBSYSTEM=="drm", HOTPLUG=="1", RUN+="/usr/local/bin/hotplug_monitor.sh"
Note: you can reload the udev rules, e.g., with “udevadm trigger”. The following script (/usr/local/bin/hotplug_monitor.sh) takes care of executing the appropriate scripts etc. when the external screen is (un-)plugged:
export DISPLAY=:0.0 function connect(){ xrandr --output VGA1 --auto --left-of LVDS1 } function disconnect(){ xrandr --output VGA1 --off } xrandr | grep "VGA1 connected" &> /dev/null && connect || disconnect
This script simply activates or deactivates the external output but different other things could be done as well.
Thank you for this! Seems to work well for me. Running Debian Squeeze on a little eeepc netbook.