I’m looking to vary some parts of my ~/.config/i3/config
file based on how many output displays are connected to my computer. So far, I could only think of two ways to do this: 1) generating the ~/.config/i3/config
with the use of a script 2) using the include
mechanism in the i3 config.
I prefer the second option, but I cannot think of any way to implement it without using some kind of environment variable that is set before an i3 session starts.
The idea is to have, say two separate config files (~/.config/i3/three-monitor.conf
and ~/.config/i3/single-monitor.conf
), which will then be included inside the main config.
Selecting the config to be included can be done by setting an environmental variable, say, DISPLAY_MODE
inside my monitor.sh
script.
#monitor.sh
xrandr --output VGA1 --above eDP1
xrandr --output HDMI1 --right-of VGA1
output=$(xrandr --listactivemonitors)
#$output would be something like:
#Monitors: xx
#0: +*eDP1....
#1: +....
# etc. etc.
number_of_active_monitors=$(echo $output | awk '{print $1}'|cut -d -f 2)
#After this, set DISPLAY_MODE to either "single-monitor" or "three-monitor" with a case statement
#Inside ~/.config/i3/config
include ~/.config/i3/$DISPLAY_MODE.conf
#etc. etc.
Where do you guys recommend I define the DISPLAY_MODE
environment variable? Is it better do it inside ~/.xprofile
, ~/etc/xprofile
, ~/.xsession
, or ~/.xinitrc
?
UPDATE:
I managed to create the environment variable by adding a line in /etc/environment
But now, I’m having trouble setting its value from within ~/.config/i3/config
I thought exec --no-startup-id source ~/.screenlayout/monitor.sh
would do the trick, but it didn’t. The environment variable DISPLAY_MODE
exists, but its value wasn’t changed.
The contents of ~/.screenlayout/monitor.sh
look something like this:
#!/bin/sh
xrandr --output eDP1 --primary --mode 1920x1080 --pos 0x1080 --rotate normal --output DP1 --off --output DP2 --off --output HDMI1 --mode 1920x1080 --pos 1920x0 --rotate normal --output HDMI2 --off --output VGA1 --mode 1920x1080 --pos 0x0 --rotate normal --output VIRTUAL1 --off --output eDP-1-2 --off --output DVI-D-1-1 --off
output=$(xrandr --listactivemonitors)
number_of_monitors=$(echo $output | cut -d " " -f 2)
if [[ "$number_of_monitors" == "1" ]]; then
DISPLAY_MODE="_single_monitor"
fi
if [[ "$numer_of_monitors" == "2" ]]; then
DISPLAY_MODE="_dual_monitor"
fi
if [[ "$number_of_monitors" == "3" ]]; then
DISPLAY_MODE="_three_monitor"
fi
I expected DISPLAY_MODE
to be set to “_three_monitor” as I’m currently on a three monitor set-up, but it didn’t.
The only way I could set DISPLAY_MODE is by sourcing it directly in my terminal:
cd ~/.screenlayout
source ./monitor.sh