1 回答
You've got this:
if which rvm-prompt &> /dev/null; then
if [ $(rvm-prompt i v g s) != "system" ]; then
rvm_ruby='%{$PR_RED%}‹$(rvm-prompt i v g s)›%{$PR_NO_COLOR%}'
fi
else
# We don't have `rvm-prompt`, try using `rbenv` instead.
fi
Let's have a look at what this is actually doing. When you open a new terminal, zsh
loads a bunch of files, including ~/.zshrc
. In your ~/.zshrc
, you're source
ing the theme
. Thus, as zsh
loads, it runs through the edit you've made.
The first thing it'll check is whether or not it can find rvm-prompt
using the zsh
[builtin] which
. It does this by checking the return code
of which
(if 0
, continue). (Not finding rvm-prompt
is a different issue, and not really related to this answer).
If we do find rvm-prompt
, using your edit we then examine whether or not the output of rvm-prompt i v g s
is system
. It's not, so we set $rvm_ruby
to contain a call to rvm-prompt
.
Then, zsh
continues loading the theme, finally finishing and setting $PROMPT
to a bunch of stuff, including a call to rvm-prompt
. It's very useful to remember that zsh
does not re-examine the theme logic! (That's why we need to source ~/path/to/theme
after editing it)!
So what's the problem? If rvm-prompt
does not say system
on shell startup, we use output from rvm-prompt
for the remainder of the session in our prompt. (If rvm-prompt
was system, we wouldn't display any rvm
indicator in our prompt... because rvm_ruby
is left blank!).
Now that we understand what's happening, we can work on fixing it. There are two approaches I can think of.
1. Strip the word system
out of the prompt:
if which rvm-prompt &> /dev/null; then
rvm_ruby='%{$PR_RED%}‹$(rvm-prompt i v g s | sed -e "s/system//")›%{$PR_NO_COLOR%}'
else
# We don't have `rvm-prompt`, try using `rbenv` instead.
fi
This works, but will display a (red) <>
in your prompt when rvm-prompt
is system
:
╭─@charmander.local ~ ‹ruby-1.9.3›
╰─ rvm default
╭─@charmander.local ~ ‹ruby-1.9.3›
╰─ rvm use system
Now using system ruby.
╭─@charmander.local ~ ‹›
╰─
You might find this useful. I think it would annoy me.
2. Use a smarter $PROMPT
: write a function.
In your theme
:
function current_rvm() {
if which rvm-prompt &> /dev/null; then
if [ $(rvm-prompt i v g s) != "system" ]; then
# The double quotes make it work, single quotes do not work.
echo "%{$PR_RED%}‹$(rvm-prompt i v g s)›%{$PR_NO_COLOR%}"
else
# `rvm-prompt` is `system`
echo ''
fi
else
# We don't have `rvm-prompt`, try using `rbenv` instead.
fi
}
local rvm_ruby='$(current_rvm)'
This should work happily for you:
╭─@charmander.local ~ ‹ruby-1.9.3›
╰─ rvm use system
Now using system ruby.
╭─@charmander.local ~
╰─ rvm use default
Using /Users/simont/.rvm/gems/ruby-1.9.3-p362
╭─@charmander.local ~ ‹ruby-1.9.3›
╰─