---
title: Run a Command Every Time You Change Directories in zsh
teaser:
tags: unix
author: Josh Clayton
published_on: 2012-02-16
---

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)"
      fi
    }

    current

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