1. Clean and simple
I think the cleanest and easiest would be to:
- Find out both screen names with the help of
xrandr (look for the string "connected")
Open System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:
xrandr --output <screen_name1> --auto --same-as <screen_name2>
To a key combination of your choice.
That way you would only have to press the key combination to activate mirrored screens.
2. Without having to look up the screen name
A bit more sophisticated would be to do the same, but let a script find out both screen names:
#!/usr/bin/env python3
import subprocess
output = subprocess.check_output(["/bin/bash", "-c", "xrandr"]).decode("utf-8").split()
connected = [output[i-1] for i in range(len(output)) if output[i] == "connected"]
command = "xrandr --output "+connected[0]+" --auto --same-as "+connected[1]
subprocess.Popen(["/bin/bash", "-c", command])
Save the script as mirror.py, run it by the command:
python3 /path/to/mirror.py
under a key combination, like in the first option.
3.Fully automatically
If you want it all done without any action from your side, run the script below in the background. It checks every 5 seconds if a second monitor is connected and sets it to mirrored if so.
The script
#!/usr/bin/env python3
import subprocess
import time
def screens():
output = subprocess.check_output(["/bin/bash", "-c", "xrandr"]).decode("utf-8").split()
return [output[i-1] for i in range(len(output)) if output[i] == "connected"]
todo = True
while True:
scr = screens()
if len(scr) == 2 and todo == True:
command = "xrandr --output "+scr[0]+" --auto --same-as "+scr[1]
subprocess.Popen(["/bin/bash", "-c", command])
todo = False
elif len(scr) == 1 and todo == False:
todo = True
time.sleep(5)
How to use
- Copy the script into an empty file, save it as
mirror_screen.py
- Test it by restarting your laptop (screen disconnected)
Run the script by the command:
python3 /path/to/mirror_screen.py
Connect the second monitor to see if it automatically switches to mirrored.
- If all works fine, add the command (above) to your startup applications:
Dash > Startup Applications > Add