You can say “the last component” with %1d
and “the first component” with %-1d
but there's no way to say “all components except the last one”.
You can use a variable expansion instead of a prompt escape sequence.
setopt prompt_subst
PROMPT='${${PWD//#$HOME/~}%/*} (%1d) %#'
You may want to tweak this for the root directory and directories immediately underneath, and for the home directory and directories immediately underneath.
Alternatively, you can recalculate this part of the prompt on each directory change. This may be useful or overkill in your case, depending on how much you want to customize the special cases.
setopt prompt_subst
PROMPT='$psvar[1] %#'
chpwd_prompt () {
local HPWD=${(%)${:-%~}} # $PWD with ~ abbreviations
case $HPWD in
*?/*) psvar[1]="${HPWD%/*} (${HPWD##*/})";;
*) psvar[1]="($HPWD)";;
esac
}
chpwd_functions+=(chpwd_prompt)
# Trigger the chpwd hooks once
cd .
(:h)
, for example like hereecho path/to/dir(:h)
. But I dont know how to incorporate this in the command prompt string.