How can I automatically switch off internal screen when an external screen is connected?

Windows user over here, jumping on the Ubuntu train. Currently using 14.04. I have a TV that I use for viewing movies and such, connected through HDMI. I’d like to accomplish the following behaviour but I haven’t figured out yet

  • On HDMI connect, do not display anything on my laptop, just display on the TV (automatically)
  • On HDMI disconnect, display back my laptop

Only tinkering through settings I can achieve that but it’s extremely manual. Am I missing something?

Thanks a lot!

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Automatically switch off your internal screen if an external screen is connected

This can be easily done by running a small background script (started automatically on startup (log in).

What it does

The script will automatically switch off your internal screen if your TV (or any other secundary screen) is connected. If you only want it to work for a specific external screen, please mention.

The script

#!/usr/bin/env python3
import subprocess
import time
# --- set your internal screen below (the example is my primary screen)
internal = "DVI-I-1"
#---

# don't change anything below
scr_info1 = 0

while True:
    time.sleep(4)
    # read the current screen setup from xrandr
    get_screens = subprocess.check_output("xrandr").decode("utf-8").splitlines()
    scr_data = [l for l in get_screens if " connected " in l]
    # count the number of connected screens
    scr_info2  = len(scr_data)
    # if the number of connected screens changes, 
    # switch off the internal one if there are two screens
    if scr_info2 != scr_info1:
        if scr_info2 == 2:
            ext = [s.split()[0] for s in scr_data if not internal in s][0]
            subprocess.Popen(["xrandr", "--output", internal, "--off", "--output", ext, "--auto"])
    scr_info1 = scr_info2

How to setup

  1. Copy the script below into an empty file, save it as switch_screens.py
  2. In the head section of your script, set the name of your internal screen. To find out, open a terminal window and run the command xrandr (no external screen connected) The line with “connected” in it shows the name of your screen in the first string, looking like: VGA-1 or something like that.
  3. Test- run it by opening a terminal window and typ the command:

    python3 /path/to/switch_screens.py
    

    While the script runs, connect your TV, wait for you internal screen to switch of and disconnect again.

  4. If all works fine, add the command below to Startup Applications: open Dash > Startup Applications > Add. Add the command:

    /bin/bash -c "sleep 15 && python3 /path/to/switch_screens.py"
    

Log out and back in. Now your internal screen is switched off automatically if an external screen is connected.

Solution 2

Unity normally remember the settings like you describe. That means if you once activate your TV and afterwards deactivate your laptop screen this should done next time you connect your TV again. Also if you unplug your TV it should go back to the Laptopsreen.

As you say this didn’t happen you can make a small batch file which activate second screen if it is connected and deactivate your laptop screen.
This is linked to that question

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply