'How to use zsh vcs_info with double quoted prompt?

this is my .zshrc file . vcs_info working for single quoted prompt but it's not working for double quoted prompt.

autoload -Uz vcs_info
zstyle ':vcs_info:*' enable git svn
zstyle ':vcs_info:git*' formats "- (%b) "
precmd() {
    vcs_info
}

setopt prompt_subst

# prompt='%2/ ${vcs_info_msg_0_}> '
prompt="%2/ ${vcs_info_msg_0_}> "

but this not working.



Solution 1:[1]

you just need change your syntax to use vcs_info in double quoted prompts

prompt="%2/"'${vcs_info_msg_0_}>'

or here is another example (parrot os prompt)

prompt="%F{red}?[%f%F{green}%n%f%F{yellow}?%f%F{cyan}%m%f%F{red}]?[%B%F{magenta}%~%f%F{red}]%f%F{201}"'${vcs_info_msg_0_}'"%f"$'\n'"%F{red}??%f%F{yellow}$%f"

Solution 2:[2]

Inside double quotes, the parameter expansion is expanded immediately, so you are hard-coding the value of ${vcs_info_msg_0_} when the prompt string is defined, rather than the value when the prompt is displayed.

Your options include

  1. Continuing to use single quotes

  2. Escaping the $: prompt="%2/ \${vcs_info_msg_0_}> "

  3. Setting prompt itself inside precmd, so that prompt is freshly defined after each update to the VCS information.

     precmd () {
         vcs_info
         prompt="%2/ ${vcs_info_msg_0_}> " 
     }
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 chepner