How to Reload i3status config On the Fly


Sometimes I want to change the status (i3status) of i3wm temporarily, but it seems that i3wm doesn't support it directly, although reloading the config for i3wm itself is a piece of cake (bindsym $mod+Shift+c reload in the config, or i3-msg -t command reload in the command line).

But this issue scratched my itch, and I swear that I must solve it today.

Following this Reddit post, it looks like it can be done by restarting the process of i3bar:

  1. Add a bar id (e.g., bar-0) and reload your config.
  2. Add a binding for killall i3bar; i3bar --bar_id=bar-0

But I failed to make it work on my side, with something like below in a specific mode in i3's config: bindsym p exec --no-startup-id killall i3bar; i3bar --bar_id=bar-0

And tweaking the config in other ways didn't help too. So it seems it came to a dead end.

But suddenly, I think that maybe I can give it a try to invoke the commands via i3-msg, the RPC client to i3wm. And following that rabbit hole, I finally made a shell script to do it.

The fundamental is to restart the i3bar process:

function i3status_reload()
{
    i3-msg -t command 'exec --no-startup-id killall i3bar'
    i3-msg -t command 'exec --no-startup-id i3bar --bar_id=bar-0'
}

And, here is the whole script, say, ~/bin/i3status_reload.sh :

#!/bin/sh

if [ $# -ne 1 ]; then
    echo "Usage: $0 on/off"
    exit 1
fi

function i3status_reload()
{
    i3-msg -t command 'exec --no-startup-id killall i3bar'
    i3-msg -t command 'exec --no-startup-id i3bar --bar_id=bar-0'
}

ymd="%Y-%m-%d"
config=~/.config/i3status/config

if [ "$1" == "on" ]; then
    sed -i "s/^.*$ymd.*$/        format = '%Y-%m-%d %H:%M:%S'/" $config
else
    sed -i "s/^.*$ymd.*$/        format = '%Y-%m-%d'/" $config
fi

i3status_reload

When I want to execute it, I just run the command from a shell. Or, if you like, you can also bind it to a key in i3wm, for example, bindsym $mod+s exec --no-startup-id sh ~/bin/i3status_reload.sh

Reference:

i3wm  Linux 

See also

comments powered by Disqus