So my current prompt looks like this:
~/path/to/dir %

If possible, I'd like it to show the topmost dir separately, like this:
~/path/to (dir) %

I've gotten close! In my .zshrc, I currently have: PROMPT="%~ (%1d) %#" which outputs:
~/path/to/dir (dir) %

I just need to remove the last folder "dir" from the first path. Any ideas?

(Note: I asked this question in Stack Overflow and got redirected here.)

  • One idea to remove the tail is using (:h), for example like here echo path/to/dir(:h). But I dont know how to incorporate this in the command prompt string.
    – user11319
    Nov 28, 2018 at 19:26

1 Answer 1


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)";;
# Trigger the chpwd hooks once
cd .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.