Reputation: 12615
Can anyone show how to modify the following zsh prompt environment var assignment to make its emitted string end with $
(followed by a space), instead of %
?
export PROMPT='%F{111}%m:%F{2}%~ %#%f '
macOS Catalina changed the default shell to zsh, and I saw an article that encouraged a switch. I assumed backward compatibility but prompt logic changed.
Upvotes: 3
Views: 2224
Reputation: 10250
%(!.#.$)
The portion of your prompt string that's generating the %
is %#
-- which generates #
if the shell is a privileged (root) shell, and %
otherwise.
If you still want that general class of behavior, just with $
instead of %
, you can do %(!.#.$)
in place of %#
. This is a conditional, with three parts (each separated by .
): !
checks to see if the shell is privileged, the #
is the expansion value if the check comes back true, and the $
is the expansion value if it's false.
A demonstration session (in a shell with a starting prompt of simply %
(PROMPT='%% '
)):
% PROMPT=': %#; ' ;: # the default %# (unwanted %, just to show it)
: %; sudo -s ;: # prompt is % now -- until we switch to root
: #; exit ;: # prompt is # now -- exit to return to user
: %; PROMPT=': %(!.#.$); ' ;: # set new prompt string with conditional for #/$
: $; sudo -s ;: # prompt is $ now -- what we wanted
: #; exit ;: # prompt is # for root shells, still
: $; ;: # back to user shell, prompt back to $
Folding this in to your prior settings, we have:
export PROMPT='%F{111}%m:%F{2}%~ %(!.#.$)%f '
Should do the trick!
(As a random side-note, I'm using ;
and the builtin :
command to effect comments where they might not otherwise be allowed -- and in particular, to treat the prompt itself as a comment, so it can be copy/pasted along with a command to re-run the command -- at the expense of some noise in your shell where you paste it. Also, the above example assumes sudo has been run recently enough, and/or is otherwise configured, to not require a password -- of course, if it did, this would all still work, it would just be extra noise in the example.)
Upvotes: 2
Reputation: 1743
export PROMPT='%F{111}%m:%F{2}%~ $%f '
For more options on zsh prompt string customization - http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Login-information
Upvotes: 2