Turn off laptop display when HDMI is plugged-in (external display primacy)

DisplayUbuntu

Running on the the display that is needed will save power, reduce thermal load on your laptop heat pipes and fans. This will also conserve the battery and extend the life of your display if it’s only needed when you’re on the go.

This guide is tested with Ubuntu 22.04

Create a new file ‘autostart_display.sh’ in the root of your home folder with the following contents. Use ‘xandr’ command to confirm the display name used for your monitor. In this case it is HDMI-1-0.

 #!/bin/bash
    
    # Get the name of the connected HDMI output
    HDMI_OUTPUT=$(xrandr | grep "HDMI-1-0 connected" | cut -d ' ' -f 1)
    
    # Check if HDMI is connected
    if [ -n "$HDMI_OUTPUT" ]; then
        # Enable HDMI-A-0 and disable laptop monitor (eDP)
        xrandr --output $HDMI_OUTPUT --auto --primary --output eDP --off
    else
        # Enable laptop monitor (eDP) and disable HDMI-A-0
        xrandr --output eDP --auto --primary --output HDMI-1-0 --off
    fi

Save the script and make it executable:

chmod +x autostart_display.sh

Now add this to your startup by using Cinnamon, XFCE, Wayland, Gnome interface to create a new startup command.

Alternatively, add a line in .bashrc that will run on startup. Enjoy!

~./autostart_display.sh

References:

https://superuser.com/questions/701716/disabling-laptop-monitor-when-external-hdmi-monitor-is-plugged-in