zsh不会重新计算我的shell提示符
发布时间:2020-12-13 18:41:26 所属栏目:Linux 来源:网络整理
导读:这可能有点边缘,但我最近搬到了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 ne
这可能有点边缘,但我最近搬到了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}%# " 解决方法
在zsh中自定义提示时遇到同样的问题.
我相信这是因为当初始化提示时,shell会将值插入到字符串中一次.随后的重新加载在您的提示中具有常量字符串,而不是subshel??l插值. 相反,将包含subshel??ls的任何行放入用单引号定义的变量中.然后插入该变量. autoload -U colors && colors local parse_special='%{$fg[yellow]%}$(date)%{$reset_color%}' PS1="%{$fg[green]%}%n@%m %{$fg[blue]%}%c ${parse_special} %# " 更新:从ZyX的答案中添加这个,为此做一个完整的解决方案.您还需要添加以下内容: setopt promptsubst 事实上,我建议将您的提示的每个部分提取到这样的变量中,其中包括每个的一个reset_color.这样做可以让您更改提示组件的顺序,而无需更改其实现. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |