zsh not re-computing my shell prompt

后端 未结 2 377
情话喂你
情话喂你 2021-01-17 11:21

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

Part of my .zshrc looks like this:

# kee         


        
相关标签:
2条回答
  • 2021-01-17 11:39

    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.

    0 讨论(0)
  • 2021-01-17 11:41

    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.

    0 讨论(0)
提交回复
热议问题