Dynamic screen definitions in i3wm

Below we will generate a custom screens definition config file for i3 and using update-conf.py to generate the i3 config from a config.d directory.

Installing requirements

To generate a config file from a config.d, we are using the Python package update-conf.py, so lets install it.

sudo pip install update-conf.py

Separating i3 config files

Now let's create the config.d directory and put our config files into the directory.

mkdir ~/.i3/config.d
cp ~/.i3/config ~/.i3/config.d/99-main

For the screen definitions we want a separate config file the following:

set $mainscreen DP-1
set $sidescreen eDP-1

Creating the gen-screen-config.sh script

This file will change if, we use a different settings of monitors or plug a monitor into a different port on the computer. To update this file we will use the following script.

#!/bin/bash

xrandr --listactivemonitors | awk '$1 ~ /^0/ {print "set $mainscreen " $4}'
xrandr --listactivemonitors | awk '$1 ~ /^1/ {print "set $sidescreen " $4}'

Generating the screen config file for i3

You can use the script like this:

./gen-screens-conf.sh > ~/.i3/config.d/10-screens

Generating the i3 config from config.d files

To generate the i3 config file from the config.d directory, we use the update-conf.py:

update-conf.py -f /home/maik/.i3/config

This will concatenate all files in config.d directory into the i3 config file.

Optimizing the process

To make it easier to switch between setups, we can integrate the calls into i3 restart binding.

To not mess up the i3 config and to make sure the screens config config is generated when we regenerate the i3 config, we create enother script called refresh-screensettings.sh.

#!/bin/bash

~/.i3/scripts/gen-screens-conf.sh > ~/.i3/config.d/10-screens
update-conf.py -f ~/.i3/config

With this script in place we can use it in the restart binding as follow:

bindsym $mod+Shift+r  exec /home/john/.i3/scripts/refresh-screensettings.sh; restart

Now, every time we change the setup, we just need to restart i3. Make sure you have set the primary screen before restarting i3. You can do that for example with the ARandR program.

By @MrTango in
Tags :