Run a Command Every Time You Change Directories in zsh

Josh Clayton

You can run a command every time you change directories in zsh with the chpwd function:

export CURRENT_PROJECT_PATH=$HOME/.current-project

function chpwd {
  echo $(pwd) >! $CURRENT_PROJECT_PATH

current() {
  if [[ -f $CURRENT_PROJECT_PATH ]]; then
    cd "$(cat $CURRENT_PROJECT_PATH)"


This will write the current directory to a hidden file in $HOME; opening a new terminal will automatically cd to that directory.