20

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:

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

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 % 

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.


reply to cjhveal

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

And got this output

#inner stuff was green
PS1 set by tp1: %{%}%n@%m%{%}
#everything was uncolored
PS1 set by tp2: %{$fg[green]%}%n@%m%{$reset_color%}

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

2 Answers 2

25

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.

6
  • thanks for the response. I feel like this is the right solution but I must still be doing something wrong (see the edit I made to my original post). Seems like I cant use single quotes here. Also can you explain why using single quotes is important in your reply? is var="$(do_stuff)" treated differently than var='$(do_stuff)' ?
    – D.C.
    Aug 9, 2012 at 7:08
  • Try removing the %{%} wrapped around the subshell. I don't know why, but it works for me without them. The difference between single and double quotes is in how the shell performs the interpolation. var="$(do_stuff)" immediatly executes the subshell and interpolates the result once. When you use single quotes, that subshell is not interpreted, and stays as a literal constant until the moment it is interpolated into a string with double quotes. Then it is executed. Essentially, it waits until the prompt is refreshed to execute the subshells.
    – cjhveal
    Aug 9, 2012 at 14:06
  • 2
    @cjhveal %{ %} enclose the text that zsh should consider to be zero-width (normally sequences that tell terminal to do some thing with the following text like changing its color). If you enclose non-zero-width text in them you will still have it displayed, but with a number of bugs (for example, when you do complete zsh tells the terminal it wants cursor on certain position and puts text there. %{non-zero-width-text%} will make this position wrong and you get text that overwrites prompt and is colored in prompt color).
    – ZyX
    Aug 9, 2012 at 17:18
  • 1
    See ZyX answer as well, I was missing 'setopt promptsubst' which is a critical piece. Also see stackoverflow.com/questions/11886412/… to complete the loop on this.
    – D.C.
    Aug 9, 2012 at 17:20
  • I found the problem, it was due to double quotes. Use single quotes then it works. So it should be PS1='......' Jul 28, 2020 at 17:35
12

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.

1
  • ah, yeah setting setopt promptsubst was the piece I was missing. Thanks a lot. I'm going to edit the above answer to add that line to make a complete answer out of these two. Thanks again.
    – D.C.
    Aug 9, 2012 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.