This page looks best with JavaScript enabled

My Polybar Tips and Tricks

 ·   ·  โ˜• 8 min read · ๐Ÿ‘€... views

Why choose Polybar

Trust me, there are open source bars in the world in a number you can’t even imagine. Also, you can create your own bar instead! (OldTechBloke has a good demonstration).

  1. Lemonbar
  2. i3 bar
  3. Conky (yes, you can use this as a bar also)
  4. Candybar
  5. Dzen
  6. Tint2
  7. n30f
  8. Cairo-dock
  9. xmobar

I have just taken a look at lemonbar and i3 bar and found that polybar is best suited for me. Below are the reasons I like polybar the most.

  1. Very easy to configure. You do not need to know any language (but knowing how .ini files work will be a bonus).
  2. Is extensible with IPC (Inter Process Communication). So you can put any command’s output or trigger a thing for any event in the OS.
  3. The built in modules are very useful and may be all that you need to put into a bar.
  4. The maintainers are great! Put any issue you want and they’ll respond very fast.
  5. You can choose your font and customize it’s size, offsets, thickness etc. to give it the look and feel you want.

My polybar setup

My polybar configuration can be found on my dots repository. This setup is highly inspired by the famous post in r/unixporn by juker97.

Below is an image of my complete polybar setup which I use regularly.

Polybar with 4 backgrounds

My complete desktop:

Desktop

The directory structure I maintain in my ~/.config/polybar is given below:

~/.config/polybar/
โ”œโ”€โ”€ scripts/
โ”‚   โ”œโ”€โ”€ corona.sh
โ”‚   โ”œโ”€โ”€ cpu.sh
โ”‚   โ”œโ”€โ”€ power-menu.sh
โ”‚   โ”œโ”€โ”€ power-menu.rasi
โ”‚   โ””โ”€โ”€ ...
โ”œโ”€โ”€ autostart
โ””โ”€โ”€ config

As you can see, the default location of the config file of polybar is kept same. I keep all the scripts related to polybar in the scripts folder. The autostart script is a bash/zsh script I use to run multiple polybars with only one command (that is running the script only)

My autostart file is something like this.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/bin/sh

# Terminate already running bar instances
killall -q polybar

# Wait until the processes have been shut down
while pgrep -u $UID -x polybar >/dev/null; do sleep 1; done

# Create gap for polybar
bspc config bottom_padding $(($(bspc config bottom_padding)+35))

# Start all the bars
polybar launcher &
polybar workspace &
polybar systray &
polybar status &
polybar power &
The below tips and tricks are written in such a way that it assumes you have basic knowledge of how linux and polybar works. If you do not know what polybar is or do not know what linux environments are and how they work, then go ahead and learn those first. You’ll find tons of tutorial and documentations which will be thousands times better than a tutorial I will ever write.

The Tips

1. Kill all polybar instance before creating a new instance.

Whenever you are starting/restarting your polybar instance, it is a good idea to kill the previous instance first. To do that, you can run the following command to kill all the instances at once.

1
killall -q polybar

Sometimes, you may need to wait for the instance to kill before starting the new instance. Run the following command to wait for that moment.

1
while pgrep -u $UID -x polybar >/dev/null; do sleep 1; done
This might sometimes wait forever if killall -q polybar could not kill all instances of polybar correctly. Though it never happened to me.

2. Show polybar in the desired monitor you want

Polybar has an option in config file named monitor which can be used with a bar. For more details about it, refer to Bar settings section in the polybar wiki. I am adding this because before seeing in the page, I sort of accepted that polybar bars can only be in the primary monitor.

Below is a simple example:

1
2
[bar/abar]
monitor = YOUR_MONITOR_NAME_HERE

3. Use the IPC as less as possible

The IPC (Inter Process Communication) is one of the most attractive features of Polybar. Basically what it does is it waits for another outside process to trigger a hook stored in the polybar and run a bash command (sort of like a daemon). But it takes comparatively a lot of CPU to keep listening for a hook to trigger. Most of the built-in modules will do what you want to achieve with a very low resource requirement. So use the IPC if you absolutely need to use.

4. You can use the Xresources in your polybar

To match the color scheme of all the desktop applications (and wallpaper), a lot of people use environment variables or Xresources to store and use their color schemes. Many use Base16 color schemes in their entire rice.

Polybar has its own configuration file which is not bash or any programming language. You have the option just to use it to do whatever you want. Good news is, polybar has built-in support for Xresources with the help of xrdb. Below is an example how you can load the color scheme in polybar config.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[colors]
; wpgtk colors
background      = ${xrdb:color8:#FF1C2C43}
background-alt  = ${xrdb:color0:#FF0e1827}
foreground      = ${xrdb:color15:#FEFEFE}
foreground-alt  = ${xrdb:color9:#9ac4ff}
primary         = ${xrdb:color10:#fff}
secondary       = ${xrdb:color1:#a9b3c2}
alert           = ${xrdb:color9:#bd2c40}

; Use these ase ${colors.background}
I may be telling polybar config as an ini file, but it is not. But they look very similar to each other.

The Tricks

1. Dynamically select monitor to display polybar

Polybar config can read environment variables with the help of special syntax. You can take any environment variable with ${env:VAR_NAME:}. So, you can take a variable with the name of your desired monitor, and use it inside polybar with the monitor = ${env:MONITOR:} syntax. You can use this with the help of xrandr or other wrappers of xrandr (like arandr/autorandr) to send polybar to desired monitor.

The below example will be very useful to you if you are using a laptop and changing your attached monitors setups frequently.

These codes will do the following: If there is a second monitor found, start polybar in that monitor, else fallback to run the polybar bar in the primary monitor. You can also adjust more granular components (like height and width depending on the monitor resolution) with more environment variables.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
[bar/topright-extras]
monitor = ${env:MONITOR:}

width = 400
height = 25
fixed-center = true
bottom = false

background = ${colors.background}
foreground = ${colors.foreground}
border-color = #000000

font-0 = BreezeSans:style=Medium Condensed
font-1 = Material Design Icons Desktop

modules-left = corona

To run this config with the proper monitor, use the following script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
eval $($HOME/.local/bin/get-mons.sh)

bspc config -m $MONITOR_1 bottom_padding $(($(bspc config bottom_padding)+35))

if [ ! -z $MONITOR_2 ]; then
	bspc config -m $MONITOR_2 bottom_padding $(($(bspc config bottom_padding)+45))
	MONITOR=$MONITOR_2 polybar topright-extras &

# Fall back to ealier monitor
else
	MONITOR=$MONITOR_1 polybar topright-extras &
fi

Here, get-mons.sh is a script which sets some variables about monitors:

1
2
3
#!/bin/sh

xrandr --listmonitors | awk 'BEGIN{count=0} NR>1 {count=count+1;split($3,a,"/");split(a[2],b,"x");print "MONITOR_"count"="$NF"\nHEIGHT_"count"="b[2]"\nWIDTH_"count"="a[1]"\n"}'

The output of get-mons.sh is something like below:

1
2
3
4
5
6
7
MONITOR_1=HDMI1
HEIGHT_1=1080
WIDTH_1=1920

MONITOR_2=eDP1
HEIGHT_2=768
WIDTH_2=1366

2. Create a double column status in polybar module

Ever wondered how you can achieve this with your polybar?

Double column example

You need some font customization to get this. This will work with any font you want!

Firstly, select your favourite font. Then use the same font twice in polybar config just with different offset values like below.

1
2
3
4
[bar/status]
font-0 = BreezeSans:pixelsize=10:style=Bold Condensed;-7
font-1 = BreezeSans:pixelsize=10:style=Bold Condensed;10
; ...

Then use the fonts in your module.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
[module/wlan]
type = internal/network
interface = ${env:PRIMARY_NETWORK_ADAPTER}
interval = 3.0

format-connected =<ramp-signal><label-connected>
label-connected = %{T1}%upspeed%%{T-}%{O-35}%{T2}%downspeed%%{T-}

label-disconnected = ๓ฐคญ
label-disconnected-foreground = #6c809e

ramp-signal-0 = %{F#ff004b}๓ฐคซ%{F-}
ramp-signal-1 = %{F#ffd200}๓ฐคŸ%{F-}
ramp-signal-2 = ๓ฐคข
ramp-signal-3 = ๓ฐคฅ
ramp-signal-4 = ๓ฐคจ

3. Do not allow polybar to cut a portion off the screen.

Sometimes you want to manage the screen space by yourself. But polybar usually takes a space if it recognizes the window manager. For example, if you use BSPWM and enable wm-restack = bspwm in your settings, then windows will leave a space at the top/bottom where you polybar lives. This will also keep the polybar always on top except full screen mode.

You can avoid this polybar’s screen space taking by using the following in your global section of polybar config. This will make sense to those who are familiar with CSS box model.

1
2
3
[global/wm]
margin-top = -100%
margin-bottom = -100%

Conclusion

So those are my most helpful tips and tricks to share with all. If you have any other good ideas, please let me know in the comments. Also ask any question about anything you might want to know in more details.

Share on

Rahat Zaman
WRITTEN BY
Rahat Zaman
Graduate Research Assistant, School of Computing