zsh not re-computing my shell prompt

怎甘沉沦 提交于 2019-12-01 15:07:26
cjhveal

I ran into the same problem while customizing my prompt in zsh.

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

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

setopt promptsubst

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.

ZyX

You are half the way to solving this problem:

PS1='$(date)'

will show you prompt $(date), but

PS1='$(date)'
setopt promptsubst

will show you prompt Thu Aug 9 21:01:53 MSK 2012 (depends on $LANG and $LC_TIME, of course).

By the way, in the newest zsh you don’t need to use %{$fg[blue]%} anymore, there is nos %F{blue} for foreground, %K{blue} for background, %f%k for resetting them and a few others, see man zshmisc, section EXPANSION OF PROMPT SEQUENCES.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!