1

I would like to list the name of my Python virtualenv in my ZSH prompt but I can't get it to work. It is listed as an environment variable (VIRTUAL_ENV). I've set up another script that properly returns just the basename of my virtual env (envTest) but trying to incorporate that into my prompt didn't work either.

Prompt config:

PROMPT="╭─${user_host} ${current_dir} [${VIRTUAL_ENV}] ${rvm_ruby} ${git_branch}
╰─%B${user_symbol}%b "

Console:

╭─user@server ~/Projects/virtualenvTest []
╰─$ echo $VIRTUAL_ENV

╭─user@server ~/Projects/virtualenvTest []
╰─$ source envTest/bin/activate
╭─user@server ~/Projects/virtualenvTest []
╰─$ echo $VIRTUAL_ENV
/home/user/Projects/virtualenvTest/envTest

I can get other environment variables to show up in my prompt (e.g. LANG).

Prompt config:

PROMPT="╭─${user_host} ${current_dir} [${LANG}] ${rvm_ruby} ${git_branch}
╰─%B${user_symbol}%b "

Console:

╭─user@server ~/Projects/virtualenvTest [en_US.UTF-8]
╰─$ echo $LANG
en_US.UTF-8
0

1 Answer 1

1

If you are actually using double quotes when defining your PROMPT then all parameters in there are substituted on definition and not each time the prompt is printed. You can prevent this by quoting $VIRTUAL_ENV differently.

As putting the whole prompt inside single quotes to prevent substitution of $VIRTUAL_ENV probably will break the rest of the prompt, try quoting just the $ by prepending a \, like this

PROMPT="╭─${user_host} ${current_dir} [\${VIRTUAL_ENV}] ${rvm_ruby} ${git_branch}
╰─%B${user_symbol}%b "

I suspect that the other parameters work because they are substituted with something that is then evaluated each time. For example, ${user_host} is probably substituted with something that contains at least the prompt escapes %n and %m with an @ in between.

${LANG} works because it is already contains the expected value when you set the prompt.

You can easily confirm this by running:

echo $PROMPT

You must log in to answer this question.

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