I bought a notebook to be flexible, but when at home I use it with external monitor, keyboard and mouse to be comfortable. Now the problem was that I couldn't tell Ubuntu I want to only use the external monitor - if it's available.

Well, I've written a one-liner that fixes that problem. If the external display is attached, it will automatically switch over to it and display the internal display. Otherwise it boots normally (leaving the internal display enabled).

I put the script into the Xsession.d directory so it gets started for every user on boot: /etc/X11/Xsession.d/98vgaonly and made it executable.

The actual script line is this: xrandr -q | grep 'VGA connected' && xrandr --output LVDS --off --output VGA --autoIt checks if the line 'VGA connected' was found in xrandr's query and then asks your X server to turn off the internal output and use the external one with automatic resolution detection.

So to automatically switch to a connected VGA during boot just execute:

You may have to change the name of the outputs for your setup. xrandr -q will let you know what's available. It would probably be possible to somehow call this script automatically when plugging in a monitor, but I've not figured out how. xrandr only works when called from inside the running X session in my experience.

I don't need to detect the external monitor at boot. Instead in my work I have to move around a lot and my laptop has a small 12.1" monitor. So I modified a little the script and added a custom keyboard shortcut in GNOME to change monitors with a single key (F12 in my case).

(note: my laptop detected the laptop display as LVSD1 and the external monitor as VGA1)

It detects if the external monitor is connected, if so it turns off the laptop screen.

The problem was that is I disconnected the external monitor, the laptop monitor stayed off and I couldn't see a thing. For that reason the second command detects if the external monitor is disconnected and turns on the laptop display.