Showing your laptop battery status in tmux

You can configure the tmux status bar to show pretty much anything you like. To include your laptop battery's current charge, all you need to do is to write a script that prints your current charge level.

I was going to write this script myself, but then I had a bright idea. I thought I'd have a quick look in Brian Hogan's recent Tmux book to see whether or not he'd thought to include this tip, and surprise surprise, he has. He also links to a rather nifty script on GitHub that will output your battery status on Linux, FreeBSD and OS X.

So here's what you do. Start by downloading the script and putting it somewhere in your PATH:

$ curl -OL https://raw.github.com/richo/battery/master/bin/battery
$ mv battery ~/bin/battery
$ chmod +x ~/bin/battery

The battery script will only print any output if you pass it a command line option of Charging or Discharging.

$ ~/bin/battery
$ ~/bin/battery Discharging
51%

You can also configure it to print a row of hearts (instead of the percentage) by setting the CUTE_BATTERY_INDICATOR environment variable. Neat.

Now add this line to your .tmux.conf file (it should all be on one line, but I've split it here for formatting purposes):

set -g status-right "$(~/bin/battery Discharging; ~/bin/battery Charging) \
    | %e %B %R"

Reload your Tmux config (if you're not sure how to do that without restarting Tmux, this would be a perfect time to go and buy Brian's ebook -- he tells you on page 18).

The characters on the end set the format of the date and time. Those are the settings I use; if you want to change the format refer to the strftime(3) man page for alternative format characters.

I love feedback and questions — please feel free to get in touch on Mastodon or Twitter, or leave a comment.