# Zsh prompt improvements

> Show a truncated path in your prompt.

The full path to an ID folder can be long.

```
/Users/john/Documents/P76 Johnny's personal system/20-29 Create & collect/21 Imaginarium 🧠/21.13 Flight records & boarding passes
```

This wraps over multiple lines in my prompt and looks messy. And most of that data is superfluous. The only thing I need is the last part, `21.13 Flight records & boarding passes`. It encodes everything else.

These scripts use the [environment variable standards](/jdhq/environment-variable-standards/).

## Single system

If you only have one system, define `JD_PATH`. Drop this in `~/.zshrc`.

```zsh
# ⚠️ Set your own path
export JD_PATH="/Users/john/Documents"

_jd_pwd() {
  if [[ "$PWD" != "$JD_PATH" && "$PWD" != "$JD_PATH"/* ]]; then
    echo "${PWD/#$HOME/~}"
    return
  fi

  if [[ "$PWD" == "$JD_PATH" ]]; then
    echo "~"
    return
  fi

  local rel="${PWD#$JD_PATH/}"
  local -a parts=("${(@s:/:)rel}")
  local anchor_idx=0 i=1
  for part in $parts; do
    [[ "$part" == [0-9]* ]] && anchor_idx=$i
    ((i++))
  done

  if (( anchor_idx == 0 )); then
    echo "…/$rel"
  else
    echo "…/${(j:/:)parts[$anchor_idx,-1]}"
  fi
}

PROMPT='%B$(_jd_pwd)%b $ '
```

## Multiple systems

For [multiple systems](/documentation/multiple-systems-overview/), define one path per system and the prompt prefixes the system identifier.

```zsh
# ⚠️ Set your own paths
export JD_D25_PATH="/Users/john/Documents/D25"
export JD_P76_PATH="/Users/john/Documents/P76"

_jd_pwd() {
  local system root
  if [[ "$PWD" == "$JD_D25_PATH" || "$PWD" == "$JD_D25_PATH"/* ]]; then
    system="D25"; root="$JD_D25_PATH"
  elif [[ "$PWD" == "$JD_P76_PATH" || "$PWD" == "$JD_P76_PATH"/* ]]; then
    system="P76"; root="$JD_P76_PATH"
  else
    echo "${PWD/#$HOME/~}"
    return
  fi

  if [[ "$PWD" == "$root" ]]; then
    echo "$system:~"
    return
  fi

  local rel="${PWD#$root/}"
  local -a parts=("${(@s:/:)rel}")
  local anchor_idx=0 i=1
  for part in $parts; do
    [[ "$part" == [0-9]* ]] && anchor_idx=$i
    ((i++))
  done

  if (( anchor_idx == 0 )); then
    echo "$system:…/$rel"
  else
    echo "$system:…/${(j:/:)parts[$anchor_idx,-1]}"
  fi
}

PROMPT='%B$(_jd_pwd)%b $ '
```

## The result

Now the prompt truncates when in an ID folder. Much nicer.

```
P76:…/21.13 Flight records & boarding passes $
```

### Technical note

This executes the `_jd_pwd` function on every prompt re-draw. But it only uses zsh built-ins, so it's basically free.