Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Why are prompt expansions of %v sequences quoted in bindkey style?
- X-seq: zsh-users 29546
- From: Marlon Richert <marlon.richert@xxxxxxxxx>
- To: Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx>
- Cc: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: Why are prompt expansions of %v sequences quoted in bindkey style?
- Date: Tue, 23 Jan 2024 20:43:54 +0200
- Archived-at: <https://zsh.org/users/29546>
- In-reply-to: <CAN=4vMoUnO=v3tWyY3+4ixMEfu_8_bC+c_KEOVCoLK7QH4Kogw@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- References: <CAN=4vMoUnO=v3tWyY3+4ixMEfu_8_bC+c_KEOVCoLK7QH4Kogw@mail.gmail.com>
I missed that a 2nd expansion was needed in my example, but that is easy to work around.
However, the quoting seems to make it impossible to use $psvar for anything involving control characters.
Is there no way to work around this?
I would like to find a solution that does not involve promptsubst.
> On 23. Jan 2024, at 15.05, Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx> wrote:
>
> 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