zsh-lastpwd

July 16, 2026 · View on GitHub

A tiny Zsh plugin that remembers the last directory you were in and restores it the next time you open a terminal.

$ cd ~/projects/my-app
$ # open a new terminal…
$ pwd
/home/user/projects/my-app   ✓

The state file follows the XDG Base Directory Specification ($XDG_STATE_HOME, defaulting to ~/.local/state/zsh-lastpwd).

Installation

Zinit

zi light itssimmons/zsh-lastpwd

Oh My Zsh

# Clone into your custom plugins directory
git clone https://github.com/itssimmons/zsh-lastpwd \
  "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/zsh-lastpwd"

# Then add to your .zshrc
plugins=(... zsh-lastpwd)

Antigen

antigen bundle itssimmons/zsh-lastpwd

Manual

source /path/to/zsh-lastpwd/zsh-lastpwd.plugin.zsh

Configuration

VariableDefaultDescription
LASTPWD_FILE${XDG_STATE_HOME:-$HOME/.local/state}/zsh-lastpwdPath to the state file

Set the variable before loading the plugin to customise the location:

export LASTPWD_FILE="$HOME/.zsh-lastpwd-state"
zi light itssimmons/zsh-lastpwd

See docs/configuration.md for full details.

How it works

  1. Restore — on shell startup, if the state file is readable, the plugin runs cd to the saved directory (errors are silently discarded).
  2. Save — a chpwd hook writes $PWD to the state file every time the directory changes, plus once at startup.

The plugin is intentionally minimal: four small source files in lib/ with no external dependencies.

Documentation

Contributing

Bug reports and feature requests are welcome via GitHub Issues.

Pull requests are also welcome. Please run the test suite before submitting:

zsh test/install.zsh
zsh test/smoke.zsh

License

MIT