Change the separator in Zsh %_

promptzsh

When setting PROMPT2, I can add %_ to show for if or for then when I'm in these constructs. Is there any way to change the space to be something else, e.g. show for/if or for:if or even for -> if instead of the default?

Best Answer

With the promptsubst option enabled, you could do:

$ PS2='${${(%):-%_}// / -> }> '
$ for i
for> if
for -> if>

${(%)...} enables prompt expansion on the expansion of .... ${:-text} allows to have a ${...} expand arbitrary text. And we apply the ${x//pat/reply} substitution on it. That assumes the expansion of %_ doesn't itself include % characters.