The issue that the screen layout doesn't stick is most likely the result of a minor bug, however:
You have two options:
1. Run a background script to run your command automatically once the second screen is connected:
#--- set both commands (connect / disconnect) below
connect_command = "gedit"
disconnect_command = ""
# function to get the output of xrandr
def get(cmd): return subprocess.check_output(cmd).decode("utf-8")
# - to count the occurrenc of " connected "
def count_screens(xr): return xr.count(" connected ")
# - to run the connect / disconnect command(s)
def run_command(cmd): subprocess.Popen(cmd)
# first count
xr1 = count_screens(get(["xrandr"]))
# second count
xr2 = count_screens(get(["xrandr"]))
# check if there is a change in the screen state
if xr2 != xr1:
if xr2 == 2:
# command to run if connected (two screens)
run_command(["xrandr", "--output", "VGA-1", "--auto", "--above", "LVDS-1"])
elif xr2 == 1:
# command to run if disconnected (one screen)
# uncomment run_command(disconnect_command) to enable, then also comment out pass
# set the second count as initial state for the next loop
xr1 = xr2
The script is an outtake of this one.
To use it
- Copy the script into an empty file, save it as
Test-run it by the command:
If all works as it should, add to Startup Applications: Dash > Startup Applications > Add. Add the command:
/bin/bash -c "sleep 15 && python3 /path/to/setup_scr.py"
2. Simply add the command to a shortcut, press it after you connect the second screen:
Add shortut key: Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:
- The script covers both running commands on connect- and disconnecting a screen, I left it as it was, so you have extra options.
- I couldn't test, since I don't have a second screen attached currently. It should work fine, but I am sensitive to blind spots and typos...
The script, as it is, assumes no errors will occur, which means the defined screen should be the only (external) screen you use. If that is not the case, we should build in either a conditional or a try/except. If so, please mention.
In case of errors, please let me know.