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).
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.
- Very easy to configure. You do not need to know any language (but knowing how .ini files work will be a bonus).
- 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.
- The built in modules are very useful and may be all that you need to put into a bar.
- The maintainers are great! Put any issue you want and they’ll respond very fast.
- 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.
My complete 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.
|
|
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.
|
|
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.
|
|
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:
|
|
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.
|
|
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.
|
|
To run this config with the proper monitor, use the following script:
|
|
Here, get-mons.sh
is a script which sets some variables about monitors:
|
|
The output of get-mons.sh
is something like below:
|
|
2. Create a double column status in polybar module
Ever wondered how you can achieve this with your polybar?
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.
|
|
Then use the fonts in your module.
|
|
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.
|
|
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.