Add a mail indicator to the tmux status bar

tl;dr: add the following to your ~/.tmux.conf, and start a new tmux session.

MAIL_ICON=" #(cat /var/spool/mail/$USER | grep ' ' && echo '✉') "
set -g status-right "$MAIL_ICON"
set -g status-interval 60

tmux status bar


While in town, I usually have a tmux session with chat and 1-2 other applications open. Conversely, I do not write town mail very often, and may go for weeks or months forgetting to check my inbox. In this situation, it is helpful to have a mail indicator to alert me whenever there is a new message.

The main piece to add to your tmux configuration (by default ~/.tmux.conf) is:

MAIL_ICON=" #(cat /var/spool/mail/$USER | grep ' ' && echo '✉') "

This line checks the user mail spool and outputs a little envelope icon if it finds a space character in the file contents (a properly formatted mail message includes headers and plenty of space characters). When you open Alpine or fetch mail from another client, the mail client will copy all messages to the inbox and the mail file will be empty again.

You can set it to display on the left or right side of the status bar: set -g status-left "$MAIL_ICON" or set -g status-right "$MAIL_ICON". set -g status-interval [seconds] determines the refresh rate, including any other system information indicators in the bar. My status-interval is set to 60 seconds, which goes well with a clock displayed in [hours]:[minutes] in my status bar, and my disk usage does not change significantly to warrant more frequent monitoring.

If you come upon this post and the indicator does not work, the most likely explanation is the mail file location is incorrect or has changed. In some systems it might be /var/mail/$USER or a custom $MAIL variable. Check with your friendly local admin for the correct path.

The variants below did not work.

MAIL_ICON=" #(cat $HOME/mbox | grep 'Status: O' && echo '✉') "

This looks in Alpine's mbox for unread messages. Alpine has to fetch the mail and add it to the mbox, and if Alpine isn't running, the mbox won't be updated.

MAIL_ICON=" #(has_mail=`cat /var/spool/mail/$USER`; test -z $has_mail || echo '✉') "

This is valid Bash, maybe tmux did not like custom variables. I also tried rephrasing it with the if conditional instead of custom variables, but no go either. It should work if it were moved to its own script file and called from within the tmux config, but I did not want to add another file if there is a simpler solution using only the tmux config.

Updated:  2022-03-26 14:40 UTC