这可能有点边缘,但我最近搬到了 zsh 并且在自定义我的 shell 提示时遇到了问题。
我的 .zshrc 的一部分如下所示:
# keeping this simple right now by just printing the date, but imagine this function would look for something specific when moving to a new directory each time
function parse_special {
print $(date)
}
autoload -U colors && colors
PS1="%{$fg[green]%}%n@%m %{$fg[blue]%}%c %{$fg[yellow]%}%{$(parse_special)%} %{$reset_color%}%# "
当我启动终端时,一切看起来都很好;我的提示是我所期望的:
me@someHost ~ Wed Aug 8 22:56:22 PDT 2012 %
但是当我 cd 到另一个目录时,似乎没有再次调用我的 parse_special 函数来重新计算我的自定义提示(注意日期没有改变):
me@someHost ~ Wed Aug 8 22:56:22 PDT 2012 % cd .ssh
me@someHost .ssh Wed Aug 8 22:56:22 PDT 2012 % cd ../workspace
me@someHost workspace Wed Aug 8 22:56:22 PDT 2012 %
有什么方法可以告诉 zsh 在每次要显示提示时重新计算提示?
非常感谢您的任何建议。
回复 cjhveal
似乎 PS1 不喜欢由单引号值设置。我尝试了以下方法:
local tp1="%{$fg[green]%}%n@%m%{$reset_color%}"
PS1="${tp1}"
print "PS1 set by tp1: ${PS1}"
local tp2='%{$fg[green]%}%n@%m%{$reset_color%}'
PS1="${tp2}"
print "PS1 set by tp2: ${PS1}"
并得到了这个输出
#inner stuff was green
PS1 set by tp1: %{%}%n@%m%{%}
#everything was uncolored
PS1 set by tp2: %{$fg[green]%}%n@%m%{$reset_color%}
根据 cjhveal 的建议,我还应该补充一下,这是我真正尝试过的。再一次,单引号似乎把事情搞砸了
function parse_special {
print $(date)
}
autoload -U colors && colors
local prompt_user='%{$fg[green]%}%n@%m%{$reset_color%}'
local prompt_root='%{$fg[red]%}%n@%m%{$reset_color%}'
local prompt_dir='%{$fg[blue]%}%c%{$reset_color%}'
local prompt_special='%{$fg[yellow]%}%{$(parse_special)%}%{$reset_color%}'
PS1="${prompt_user} ${prompt_dir}${prompt_special}%# "