zsh不会重新计算我的shell提示符

zsh不会重新计算我的shell提示符,第1张

概述这可能有点边缘,但我最近搬到了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 这可能有点边缘,但我最近搬到了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 timefunction parse_special {    print $(date)}autoload -U colors && colorsPS1="%{$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 greenPS1 set by tp1: %{%}%n@%m%{%}#everything was uncoloredPS1 set by tp2: %{$fg[green]%}%n@%m%{$reset_color%}

我也应该补充一下,根据cjhveal的建议,这里是我真正尝试的.再次,单引号似乎搞砸了

function parse_special {        print $(date)}autoload -U colors && colorslocal 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 && colorslocal parse_special='%{$fg[yellow]%}$(date)%{$reset_color%}'PS1="%{$fg[green]%}%n@%m %{$fg[blue]%}%c ${parse_special} %# "

更新:从ZyX的答案中添加这个,为此做一个完整的解决方案.您还需要添加以下内容:

setopt promptsubst

事实上,我建议将您的提示的每个部分提取到这样的变量中,其中包括每个的一个reset_color.这样做可以让您更改提示组件的顺序,而无需更改其实现.

总结

以上是内存溢出为你收集整理的zsh不会重新计算我的shell提示符全部内容,希望文章能够帮你解决zsh不会重新计算我的shell提示符所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1045647.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存