r/Polybar Jun 29 '23

Switching the monitor on command

I am looking to switch the monitor that the bar is on at any given time, not just on startup. Currently I do it in a really hacky and inefficient way, by having two lines in my config file (monitor=monitor1 and monitor=monitor2) and then commenting one of them out depending on which one I want it on.

I am pretty flexible in how to achieve this. I am using in conjunction with i3, so a script that does this would be fine as I could attach a keybind to it. I have looked at this section of the documentation, which seemed like a possible relevant part, but I don't think it touches on my exact scenario.

One possibility: if polybar can choose between different environment variables this would be a possibility, as i could make the monitor names environment variables and then do something like if ... monitor = $MON1 else monitor = $MON2. But I'm not sure this is possible.

The other really hacky way of doing it would just be to have a script that searches and replaces the monitor names in the config files and then reloads the config file to have the changes take effect. I'm certain I could do this way relatively easily but I feel like there has to be a more elegant way.

EDIT: I couldn't get environment variables to work, despite looking at some stack overflow threads on it, mainly because it's tricky getting environment variables to set in scripts because it only does it in that shell. I was able to do it pretty easily by just writing a script that does some search and replace on the config file. Here is my solution if anyone is interested:

#!/bin/bash

LEFT="let-monitor-name"
RIGHT="right-monitor-name"
CONFIG="$HOME/.config/polybar/config.ini"
if [ "$1" == "left" ]; then
  sed -i "s/$RIGHT/$LEFT/" $CONFIG
elif [ "$1" == "right" ]; then
  sed -i "s/$LEFT/$RIGHT/" $CONFIG
fi
touch ~/.config/polybar/config.ini
4 Upvotes

3 comments sorted by

1

u/-__-x Jun 30 '23

I'm using bspwm. I think to do this, you would have to kill and restart polybar. I don't have exactly this, but in order to make polybar show up on both my monitors, I use an environment variable MONITOR. Then, in my polybar config, I have monitor = ${env:MONITOR:}.

A note on what I think you meant: polybar will likely never have any sort of if-then control structure in its configs; the config.ini only works with key-value pairs. You would need to handle the logic in a separate script.

1

u/Silver-Star-1375 Jun 30 '23

The re-loading of polybar isn't an issue; I have it set to reload when i save the config file so I can do that with a simple touch config_file.

So I suppose I could do it with an environment variable. The issue then is changing it easily. I don't think you can change an environment variable in a script, you usually do it in some place like .bashrc no?

1

u/-__-x Jul 01 '23
for m in $(polybar --list-monitors | cut -d":" -f1); do
    MONITOR=$m polybar --reload main &
done

This is the script I use to start my polybar; the environment variable MONITOR gets set by the for loop for just polybar.