Can I use the output from a ZSH custom function in Tmux's status bar?

I defined a function in ~/.zshrc:

foo() { print "FOO" }

I'd like to output it in my Tmux status bar. I added this to ~/.tmux.conf:

set -g status-right "foo: #(foo)"

But I only see the static portion of that; the function output is missing. (While my actual function is more complicated, this is true even for the trivial example above.)

It works fine when I call a script instead of a function. What's the difference? Is it possible to use a function there?


Solution 1:

The tmux man page states for the set option:

#(shell-command) First line of the command's output

But I'm pretty sure, that does not mean shell funcitons, because tmux will not execute the shell-command in the current shell-session, but start a new instance, which starts as non-interactive, hence does not source your ~/.zshrc config and so does not know about functions defined therein.

To work round, you could create a shell script, named foo

#!/bin/zsh -f

source ~/.zshrc   # or just the desired function to save parsing time
# use some functions defined in ~/.zshrc

print "FOO"