]> git.rmz.io Git - dotfiles.git/blob - zsh/plugins/last-working-dir/last-working-dir.plugin.zsh
5b3121d11d1992b2377cdf111f1d931a0b570bc1
[dotfiles.git] / zsh / plugins / last-working-dir / last-working-dir.plugin.zsh
1 #!/usr/bin/env zsh
2 # Keeps track of the last used working directory and automatically jumps
3 # into it for new shells.
4
5 # Flag indicating if we've previously jumped to last directory.
6 typeset -g ZSH_LAST_WORKING_DIRECTORY
7 local cache_file="$ZSH/cache/last-working-dir"
8
9 # Updates the last directory once directory is changed.
10 function chpwd() {
11 echo "$PWD" > "$cache_file"
12 }
13
14 # Changes directory to the last working directory.
15 function lwd() {
16 [[ ! -r "$cache_file" ]] || cd `cat "$cache_file"`
17 }
18
19 # Automatically jump to last working directory unless this isn't the first time
20 # this plugin has been loaded.
21 if [[ -z "$ZSH_LAST_WORKING_DIRECTORY" ]]; then
22 lwd 2>/dev/null && ZSH_LAST_WORKING_DIRECTORY=1 || true
23 fi