zsh不重新计算我的shell提示 [英] zsh not re-computing my shell prompt

查看:79
本文介绍了zsh不重新计算我的shell提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能有点麻烦,但是我最近搬到了zsh,在自定义外壳程序提示符时遇到了问题。

This might be a bit fringe, but I recently moved to zsh and am having a problem customizing my shell prompt.

.zshrc的一部分看起来像这样:

Part of my .zshrc looks like this:

# 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%}%# "

启动终端时,一切看起来都不错;我的提示是我所期望的:

When I launch terminal, everything looks good; my prompt is what I expect:

me@someHost ~ Wed Aug 8 22:56:22 PDT 2012 %

但是当我cd到另一个目录时,似乎没有再次调用parse_special函数来重新计算自定义提示(请注意日期未更改):

But when I cd to another directory, it appears my parse_special function is not called again to recompute my custom prompt (notice the date is not changing):

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每次要显示提示时重新计算提示吗?

Is there any way I can tell zsh to recompute the prompt each time it is about to show it?

非常感谢您提出任何建议。

thanks a lot for any suggestions.

回复cjhveal

似乎PS1不喜欢被单引号设置。我尝试了以下操作:

It seems like PS1 does not like to get set by single quoted values. I tried the following:

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的建议,我还应该添加以下内容,这是我实际尝试的内容。再次,单引号似乎使事情变得混乱


I should also add, based on cjhveal's suggestion, here is what I literally tried. Again, the single quotes seem to be messing things up

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插值。

I believe this happens because the shell interpolates the value into the string once, when the prompt is initialized. Subsequent reloads have the constant string in your prompt, not the subshell interpolation.

相反,将涉及子外壳的所有行放入用单引号定义的变量中。然后插值该变量。

Instead, put any lines that involve subshells into a variable defined with single quotes. Then interpolate that variable instead.

autoload -U colors && colors

local parse_special='%{$fg[yellow]%}$(date)%{$reset_color%}'

PS1="%{$fg[green]%}%n@%m %{$fg[blue]%}%c ${parse_special} %# "

更新:从ZyX的答案中添加此内容以为此提供完整的解决方案。您还需要添加以下内容:

Update: Adding this from ZyX's answer to make a complete solution for this. You also need to add this:

setopt promptsubst

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

In fact, I would suggest extracting each part of your prompt into a variable like this, including a reset_color on each. Doing so lets you change the order of prompt components without changing their implementation.

这篇关于zsh不重新计算我的shell提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆