I have a little python script to do a similar thing:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;larkery&#x2F;nix-session&#x2F;blob&#x2F;master&#x2F;scripts&#x2F;displays" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;larkery&#x2F;nix-session&#x2F;blob&#x2F;master&#x2F;scripts&#x2F;d...</a><p>This remembers the randr state that&#x27;s associated with plugged in displays by their EDID and the ACPI lid switch. When you change the randr state, it remembers it, and when you change what displays are connected or open the laptop lid, it loads a previously remembered state if there is one. The state saving and loading is done by arandr, which handles the resolution, position, and choice of primary output.