Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Why are prompt expansions of %v sequences quoted in bindkey style?



On Tue, Jan 23, 2024 at 1:07 PM Marlon Richert <marlon.richert@xxxxxxxxx> wrote:
>
> When I declare my prompt as:
>
> PS1=$'%{\e[2m%}%#%{\e[0m%} '
>
> this works as I expect it to: My prompt becomes a dim/faint % followed by a space.
>
> However, this does not work as I expect it to when I store the ANSI sequences in $psvar.
>
> The following code:
>
> psvar=( $'%{\e[2m%}' $'%{\e[0m%}' )
> PS1='%1v%#%2v '
>
> results in the following prompt with terminal default color:
>
> %{^[[2m%}%%{^[[0m%}
>
> As you can see, the strings stored in $psvar are expanded in a quoted form, à la bindkey.

Percent expansion isn't recursive. It is done only once. If %v expands
into %1F, the latter won't be expanded any further and will remain as
a literal %1F. This behavior makes sense and it alone will preclude
you from achieving what you are after.

However, in addition, the expansion of %v is quoted: newline becomes
\n, escape becomes ^[, etc. This is meant to make the use of %v safe.
By "safe" I meant that it allows you to ensure a non- broken prompt
regardless of the content of psvar.

  # This prompt is never broken: it does not bleed colors,
  # does not confuse zle w.r.t. the cursor position, etc.
  PS1='%v%# '

> Is this intentional?

I wasn't there when this feature was designed but it works as I would
expect. If you want an extra percent expansion after the substitution
of parameters, use prompt_subst. This is a much more powerful and a
much more dangerous tool.

Roman.




Messages sorted by: Reverse Date, Date, Thread, Author