1

I am a newbie of Tmux and I am now configuring the status bar in Tmux. I want my tmux status bar can show the current Conda environment like Powerlevel10K. I tried set -g status-right "conda: #{CONDA_DEFAULT_ENV} | %a %h-%d %H:%M " but it failed. The tmux status bar shows nothing in the name of current conda env. So the current right bottom corner of my tmux is like

enter image description here

but what I actually want is something like

conda: base | Wed Sep-06 23:18
2
  • Is the # sign intended or should it be $?
    – doneal24
    Commented Sep 6, 2023 at 18:27
  • You are right, if I want to return the current conda env, what I should do is #{echo $CONDA_DEFALUT_ENV}, though this method does not work. After spending several hours I finally come up with the solution and I will post it later
    – Ricky Pang
    Commented Sep 7, 2023 at 4:20

1 Answer 1

0

Resolution of TMUX showing/updating conda environment

Finally I came up with the solution of showing or updating conda environment in the Tmux status bar. I have tried to use

set -g status-right "conda: #(echo $CONDA_DEFAULT_ENV) 

but it doesn't work. It seems that Tmux does not know the $CONDA_DEFAULT_ENV. I have try similar method on $PATH and it works fine. Finally, I asked GPT4 to how to correctly show conda env name and it suggests to add following code in ~/.zshrc

# Update TMUX environment variable
# Add conda.sh to PATH
source /opt/homebrew/Caskroom/miniforge/base/etc/profile.d/conda.sh

# Update TMUX_CONDA_ENV every time a new prompt is generated
precmd() {
  if [[ -n $TMUX && -n $CONDA_DEFAULT_ENV ]]; then
    tmux setenv -g TMUX_CONDA_ENV "$CONDA_DEFAULT_ENV"
  else
    tmux setenv -gu TMUX_CONDA_ENV
  fi
}

The following is GPT4 explanation on the code snippet

We might need to take a different approach. Let's try using the 

PROMPT_COMMAND environment variable, which holds a command to be executed 

before each command prompt. We'll update the TMUX_CONDA_ENV variable 

inside the PROMPT_COMMAND so that it changes every time a new prompt is 

generated.

Now in tmux.conf we need to modify the original line to

set -g status-right "Conda: #{?TMUX_CONDA_ENV,#{TMUX_CONDA_ENV},base}"

and it works well now. It not only shows the correct conda env name, but also update the conda env name once we switch to other environment.

You must log in to answer this question.

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