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, 2017 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, 2017 at 8:45
  • The %{x.t.f%} can only test for a predefined set of conditions.
    – saga
    May 17, 2017 at 8:47

2 Answers 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

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//\%/%%}] %# '
  PS1='[not tmux] %# '

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


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


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


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


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .