Make prompt only display directory when not at $HOME?
setopt PROMPT_SUBSTPROMPT+='$([[ $PWD != $HOME ]] && echo "[%~] ")'
Explanation
setopt PROMPT_SUBST
enables parameter expansion ($name
), command substitution ($(command)
) and arithmetic expansion ($[exp]
or$((exp))
) within the prompt- PROMPT+='…' appends to the prompt. Note that you need to use single quotes here so that the contents are not expanded during definiton but only when the prompt is shown.
$(command)
runscommand
and substitutes its output, e.g.$(echo foo)
would be replaced with "foo".[[ exp ]]
evaluates a conditional expression. It will return zero only if the expression is true. Note that[[ exp ]]
is built-in syntax ofzsh
, while the similar[ exp ]
would run the external[
(akatest
) command.PWD
contains the current working directory.HOME
contains the home directory of the current user.!=
tests for inequality.&&
runs the command to the right only if the command to the left was successful (that is, returned zero)echo "[%~] "
prints the (partial) prompt string. Note that double quotes are used so that the surrounding single quotes are not closed prematurely.