Do you have trouble switching the display when docking and undocking your Linux laptop? In this article I will show you how to use disper and keyboard shortcuts to do that reliably if you are using nVidia's annoyingly broken RandR-incompatible graphics driver.
Many companies nowadays give their employees laptops instead of classical PCs under their desks. And that's a good idea because you can carry it to meetings or hack sessions and just keep all your digital information in one place instead of printing it out, carrying it around and typing it in later. And it gets even better if your employer allows you use your favorite Linux distribution instead of Windows on your laptop. Using Linux on a laptop has become increasingly simple with modern distributions like Ubuntu. But still the laptop manufacturers suck badly at providing proper drivers for anything other than Windows™. That's especially true for graphics hardware from nVidia which is the perfect example for ruining great hardware by failing to provide proper drivers. Modern desktop environments usually detect if your screen resolution changes or if you dock or undock from your laptop's dockin station. Unfortunately nVidia's proprietary driver still doesn't support common standards (RandR). They ship their own ("NVIDIA X Server Settings") tool to switch monitors which allows you to do that. Well, sometimes. At other times they alter your X server's configuration (which is something that Linux nerds used to do 10-20 years ago) and require an X server restart. Yeah, right.
But do not despair - here is a simple fix. They key is disper - a simple-to-use command-line tool to switch between different monitors. You should find it packaged for your favorite desktop Linux distribution. I personally use Xubuntu so I installed it using
$> sudo apt-get install disper
Disper is pretty simple and its manpage describes its options. Check that disper finds your monitors:
$> disper -l
display DFP-0: AU Optronics Corporation
display DFP-2: Eizo S2402W
resolutions: 640x480, 800x600, 1024x768, 1280x960, 1280x1024, 1680x1050, 1600x1200, 1920x1200
You can see here that DFP-0 is my laptop's own monitor with a resolution ov 1600x900 pixels. And I have an Eizo monitor connected to my docking station. If your output looks similar you can try to switch between the monitors:
$> disper -s (enables the "primary" display - usually the laptop's built-in monitor)
$> disper -S (enables the "secondary" display - usually the monitor connected to your docking station)
$> disper -c (clones the display - will only work if both displays can use the same resolution)
$> disper -e (extends your desktop over both monitors)
Now all you have to do is assign keyboard shortcuts for your favorite settings. I have used my Xfce keyboard settings to assign two shortcuts:
That's it. When I dock my laptop I press Win-d and the external monitor will display my screen. And when I undock I press Win-u and my desktop moves to the laptop's built-in monitor. Perfect.
(And next time please get a laptop with a well-supported Intel GPU if you can. You will love it.)