You are not logged in.

#1 2016-03-18 23:46:41

GreenRaccoon23
Member
Registered: 2014-09-27
Posts: 33

[SOLVED] Zsh PROMPT Ternary Expression Error for "%(!.true.false)"

Original title: 'Zsh PROMPT Ternary Expression Syntax Error for "%(!.true.false)"'

I'm trying to set up a custom PROMPT variable for zsh. I'm getting a syntax error when using the ternary expression syntax straight from the zsh documentation: http://zsh.sourceforge.net/Doc/Release/ … nsion.html

From the Arch wiki, the example works:

PROMPT="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg_no_bold[yellow]%}%1~ %{$reset_color%}%#"

The zsh documentation says that the `%#` at the end is shorthand for `%(!.#.%%)`. If I expand `%#` to `%(!.#.%%)`, zsh throws a syntax error message:

PROMPT="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg_no_bold[yellow]%}%1~ %{$reset_color%}%(!.#.%%)"
zsh: event not found: .#

I thought that the `#` might be a special character, so I tried this:

PROMPT="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg_no_bold[yellow]%}%1~ %{$reset_color%}%(!.root.home)"
zsh: event not found: .root.home)

Then I tried removing everything but the ternary expression:

PROMPT="%(!.root.home)"
zsh: event not found: .root.home)

And finally I tried using single characters:

PROMPT="%(!.r.h)"
zsh: event not found: .r.h)

There has to be something small I'm missing because I'm copying and pasting straight from the zsh documentation. Does anyone know what it is?

Last edited by GreenRaccoon23 (2016-03-23 01:18:51)

Offline

#2 2016-03-18 23:57:39

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,541
Website

Re: [SOLVED] Zsh PROMPT Ternary Expression Error for "%(!.true.false)"

From the very top of the page you linked to:

If the PROMPT_BANG option is set, a ‘!’ in the prompt is replaced by the current history event number. A literal ‘!’ may then be represented as ‘!!’.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2016-03-18 23:58:49

GreenRaccoon23
Member
Registered: 2014-09-27
Posts: 33

Re: [SOLVED] Zsh PROMPT Ternary Expression Error for "%(!.true.false)"

Awesome! Thanks! Do you know how I'd be able to escape the "!" then?

-----EDIT-----
Got it! I just did:

PROMPT="%(\!.root.home)";

Last edited by GreenRaccoon23 (2016-03-19 00:00:39)

Offline

#4 2016-03-19 00:06:15

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,541
Website

Re: [SOLVED] Zsh PROMPT Ternary Expression Error for "%(!.true.false)"

I'm pretty sure unsetting PROMPT_BANG or the following would also work:

...A literal ‘!’ may then be represented as ‘!!’.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2016-03-19 00:52:27

GreenRaccoon23
Member
Registered: 2014-09-27
Posts: 33

Re: [SOLVED] Zsh PROMPT Ternary Expression Error for "%(!.true.false)"

Trilby wrote:

I'm pretty sure unsetting PROMPT_BANG or the following would also work:

...A literal ‘!’ may then be represented as ‘!!’.

Yeah, this works better. Otherwise, when I load up terminal (xfce4-terminal) from scratch, the expression does not appear to evaluate and strips off the rest of the line.

So, for clarity's sake, the fix is to add this in "~/.zshrc" before setting the $PROMPT variable.

unset PROMPT_BANG;

Offline

#6 2016-03-19 12:11:54

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,541
Website

Re: [SOLVED] Zsh PROMPT Ternary Expression Error for "%(!.true.false)"

Please remember to mark your thread as SOLVED.  You might also go back and do the same for your previous threads that also have been solved.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#7 2016-03-23 01:12:45

GreenRaccoon23
Member
Registered: 2014-09-27
Posts: 33

Re: [SOLVED] Zsh PROMPT Ternary Expression Error for "%(!.true.false)"

Trilby wrote:

Please remember to mark your thread as SOLVED.  You might also go back and do the same for your previous threads that also have been solved.

Yeah when I couldn't figure out how to do that, I thought that you couldn't mark threads as solved on this forum. Apparently not. I'll try to figure that out again. It'd not very intuitive.

Last edited by GreenRaccoon23 (2016-03-23 01:16:15)

Offline

#8 2016-03-23 02:53:36

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,720

Re: [SOLVED] Zsh PROMPT Ternary Expression Error for "%(!.true.false)"

GreenRaccoon23 wrote:
Trilby wrote:

Please remember to mark your thread as SOLVED.  You might also go back and do the same for your previous threads that also have been solved.

Yeah when I couldn't figure out how to do that, I thought that you couldn't mark threads as solved on this forum. Apparently not. I'll try to figure that out again. It'd not very intuitive.

Edit your first post in the thread.  There is an option to edit the title when you edit the first post


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way

Offline

Board footer

Powered by FluxBB