~derf / interblag / entry / Automatically connecting external monitors (udev+xrandr)
dark mode

Ugly hack to automatically connect external monitors using xrandr and udev:

Write SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/libexec/some-x-script.sh" into a udev rules file. This script will (at least on my system ;-) ) now get called every time you either plug in an external VGA connector or remove it.

In this script, you can now use xrandr to set up the monitor. To find out whether it is called after connecting or disconnecting, check /sys/class/drm/card0-VGA-1/enabled (or similar). Example. Happy hacking.

By the way, note that disconnecting the monitor before deconfiguring it with xrandr may break stuff. At least my system doesn not like it and does not accept other monitors until a reboot afterwards - manually calling xrandr --off first will work, of course. YMMV.