Dynamically switch monitors with saved configuration profiles
Note: most of this article was written in 2011. It is not clear what versions of Ubuntu it applies to.
For laptop users, dynamically switching between the laptop screen and external monitors or projectors makes it much easier to get to work quickly. This page describes how to use the autorandr tool to automatically select a display configuration based on the connected devices, similar to the functionality present in Mac OS X.
Download the autorandr tar.gz package. Extract the files in it to somewhere on your path, for instance in your ~/bin directory (see Installing Software Into The Home Directory.
For users with NVIDIA graphics cards, you will need to install disper. Run the following code in the terminal:
sudo apt-add-repository ppa:disper-dev/ppa sudo apt-get update sudo apt-get install disper
Other graphics cards will use the xrandr backend. You may need to install the x11-xserver-utils package if it is not already installed. (You can check this by running xrandr --version on the command line).
sudo apt-get install x11-xserver-utils
NOTE: For NVIDIA card users, you will need to call the auto-disper command instead of autorandr. You should see auto-disper in the files you extracted from the downloaded tar.gz file. If not, simply create a link to autorandr and name it auto-disper or autodisper and the script will know to use disper for the backend.
Autorandr works by saving profiles of your different monitor configurations and restoring them on demand. When you save a profile, it detects the devices that are currently present, and the configuration that they are set up in. You can then load a specific profile manually, or you can ask it to find the appropriate profile for the devices that are present.
Save your current display configuration and setup with:
autorandr --save mobile
Connect an additional display, configure your setup and save it:
autorandr --save docked
Now autorandr can detect which hardware setup is active:
$ autorandr mobile docked (detected)
Once you have saved your profiles, autorandr will load the appropriate one for the currently connected devices with the --change option. You probably also want to use the --default option to specify a fallback if there is not a profile matching the connected devices, so that you don't get stuck with a blank screen for any reason.
autorandr --change --default mobile
You can then set this command to be run when you press a keyboard shortcut. See Assign Custom Shortcut Keys on Ubuntu.
For instance, you could set command_1 to autorandr --change --default mobile in keybinding_commands, and run_command_1 to XF86Display in global_keybindings. That would make your laptop's display key (eg. Fn+F8) run the autorandr command to reconfigure your displays on the fly.
The disper command can be used for quick and painless display configuration changes, like switching between an extended display configuration (disper -e) to using only the external monitor (disper -S). See the disper man page (man disper) or disper --help for more info on that.
For the real cherry on top, you may be able to set up a udev rule to automatically fire off your autorandr command when the kernel detects a change in monitor configuration. Unfortunately, this is not supported by many graphics drivers, so your mileage may vary. See this post for a discussion on how to go about that. (And if you get it working, describe it here!).
If you are not using a binary nVidia driver, you should be able to get this working by creating a udev rule file:
sudo nano -w /etc/udev/rules.d/70-monitor.rules
Inside that file can just be this line:
SUBSYSTEM=="drm", ACTION=="change", RUN+="/path/to/autorandr"
Finally, reload your udev rules:
sudo udevadm control --reload-rules
This assumes you have setup your 'mobile' and 'docked' autorandr profiles as described above. The rule will fire when a device in your DRM (Direct Rendering Manager) subsystem changes status, and will react by executing 'autorandr' with no arguments, which will auto-detect which profile to use based on what's currently plugged in.