0

I want the color of my zsh prompt to be decided based on whether I'm inside a tmux session or not. In bash, it can be done by checking the value of $TMUX, but I can't find an equivalent method in zsh. Is it possible in zsh?

  • The TMUX variable is independent of the shell you use. What is the issue you're having? – Kusalananda May 17 '17 at 8:38
  • In bash I could put ${TMUX:+colorcode} in the PS1 variable, to set the color, it doesn't work in zsh and I can't find a similar way to do it in the manual. – saga May 17 '17 at 8:45
  • The %{x.t.f%} can only test for a predefined set of conditions. – saga May 17 '17 at 8:47
2

In zsh, the prompt_subst option is off by default. If you want to use variable substitutions in your prompt, turn it on.

setopt prompt_subst
PS1='$foo'

For $TMUX, though, you don't need this. The value doesn't change during the session, so you can initialize PS1 when the shell starts.

setopt prompt_subst
if (($+TMUX)); then
  PS1='[tmux:${TMUX_PANE//\%/%%}] %# '
else
  PS1='[not tmux] %# '
fi

Note that prompt expansion happens after variable susbtitution, this is why the percent signs in the variable's value need to be protected.

| improve this answer | |
1

When launched tmux will inherit the environment variables of the shell that launched it (like all other processes).
From that point tmux will then bestow those same environment variables onto the pseudo terminals it creates. Except for one important environment variable

$TERM

The tmux server will hold the value give to it by the original shell.
You can query this with

<C-b>:display-message "$TERM"

on my system this gives xterm-256color.
but all pseudo terminals tmux starts will get something like

$TERM=screen

on my system I set the pseudo terminals to TERM=screen-256color so that programs (vim) running inside tmux know the color capabilities of the terminal. You can do this with this tmux command

set -g default-terminal "screen-256color"

Either way you can check for the presence of this string and set your prompt accordingly

~/.zshrc

if [[ $TERM == *screen* ]]; then
    # you're in tmux, congratulate yourself :)
    # set your tmux prompt here
else
    # you're not in tmux
    # set your fallback prompt here
fi
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.