Skip to content

Instantly share code, notes, and snippets.

@Juul
Last active July 20, 2023 02:09
Show Gist options
  • Save Juul/36b220835f5d825254e597a2a0a8631d to your computer and use it in GitHub Desktop.
Save Juul/36b220835f5d825254e597a2a0a8631d to your computer and use it in GitHub Desktop.
How to automatically change screen setup when docking/undocking

The tool autorandr can save configurations for various monitor setups and load them based on what's plugged in, or be used to manually load a saved configuration.

Use the latest version of autorandr. The version in Ubuntu 20.04 didn't work for me. To install:

sudo pip3 install "git+http://github.com/phillipberndt/autorandr#egg=autorandr"

Then for each monitor configuration, plug monitors in as needed and configure them correctly manually, then run:

autorandr --save <chosen_name_of_monitor_config>

After having at least two saved configs you can then either automatically switch to the correct setup based on what's plugged in like so:

autorandr --change

or manually load a saved configuration:

autorandr --load <name_of_monitor_config>

If you want to make this happen completely automatically when you plug in to various monitors (e.g. dock) then you need some stuff from the contrib/ dir in the git repo.

git clone https://github.com/phillipberndt/autorandr
cd autorandr/contrib/

First you need to compile and install autorandr_launcher which will listen for monitor attach/detach events and run autorandr when these occur:

sudo apt install libx11-xcb-dev libxcb-randr0-dev
cd autorandr_launcher/
make
sudo cp autorandr-launcher /usr/local/bin/

You can now try running autorandr-launcher and, while it's running, dock and undock to see the automatic monitor config switching in action.

To make autorandr-launcher auto-start when you boot into an X environment:

cd ../ # you should be in the autorandr/contrib dir again, the one from the cloned git repo
sudo cp etc/xdg/autostart/autorandr-launcher.desktop /etc/xdg/autostart/

If you want autorandr to run when you open/close your laptop lid, you'll also need to copy the autorandr-lid-listener.desktop file into you /etc/xdg/autostart directory.

and that's it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment