New laptop part 6: Managing multi screens with i3wm and autorandr

Monday, July 10, 2023

laptopframeworkautorandri3wmmultiscreen

For some time now, I’ve been using 3 external screens connected to my laptop. With my previous laptop, 3 screens was the maximum so I couldn’t use the laptop screen with the 3 external ones. A problem I don’t have anymore since I switched to the framework laptop :). But even though I could use 4 screens, I don’t. I prefer to “limit” myself to the 3 external screens. The screens are mounted on a 3 arms monitor stand similar to this one (/!\ Amazon link, sorry) and I don’t like looking down on the 4th screen.

But it is still a laptop, which means I move it a lot. Sometime just as a laptop, sometime connected to a transportable screen or a TV or others. Also sometime I go into “ultra focus mode” where I enable only the middle screen and disable the 3 others (where usually I have chat(s) and/or emails). So I need a way to switch quickly between these modes.

For that, I use xrandr to setup my screens as I want, autorandr to automate the switch to the mode corresponding to the number of screens plugged in and i3wm scripts with rofi for manual change.

To configure the screens position, I use xrandr GUI. Pretty rough UI but does the job. Once I’m happy, I save the configuration with autorandr with the following command:

autorandr --save <name>

Do that each time you have a setup you want to reuse quickly. When a screen is (un)plugged, autorandr will try to find the saved profile that match the current situation. If any, config is applied automatically (whether you want it or not :p). It is that easy :).

While it is usually good enough, it doesn’t work for when I want to go into “focus mode” (only 1 screen enabled even though 3 screens are plugged in). I don’t want to unplug screens, just disable them until I change my mind. To do so, I created a small script that I launch via a keybind (super + shift + space). This script will open a rofi pop-up asking me for one of the mode. They are hardcoded for now, I should improve that in the future to automatically find for the different existing profile in the autorandr config… But that’s for another time! Then the selected value is used within a i3-msg command to start the right autorandr command. The content of the script is as follow (or always up to date version on sourcehut):

#!/usr/bin/env bash

# Define an associative array:
typeset -A mode
# And its content:
mode=(
  [Home]="autorandr --load home"
  [Laptop]="autorandr --load laptop"
  [Focus]="autorandr --load focus1screen"
  [Presentation]="autorandr --load dualprez"
  [4screens]="autorandr --load 4screens"
  [Cancel]=""
)

mode_nrows=${#mode[@]}

selected="$(printf '%s\n' "${!mode[@]}" | sort | rofi -dmenu -p "Monitor setup" -i -lines 2)"
i3-msg -q "exec --no-startup-id ${mode[${selected}]}"

And voilà, pressing the super + shift + space keybind, I have the above pop-up asking me for the mode to use:

Figure 1: A rofi pop-up asking for different autorandr profiles

Figure 1: A rofi pop-up asking for different autorandr profiles


Contact

If you find any issue or have any question about this article, feel free to reach out to me via email, mastodon, matrix or even IRC, see the About Me page for details.

See Also

Manga collections with orgmode, orgtable and orgplot

New laptop part 5b: Prevent sleep / hibernation when home and in charge