]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/last-working-dir/last-working-dir.plugin.zsh
initial
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / last-working-dir / last-working-dir.plugin.zsh
1 # Flag indicating if we've previously jumped to last directory
2 typeset -g ZSH_LAST_WORKING_DIRECTORY
3
4 # Updates the last directory once directory is changed
5 autoload -U add-zsh-hook
6 add-zsh-hook chpwd chpwd_last_working_dir
7 chpwd_last_working_dir() {
8   # Don't run in subshells
9   [[ "$ZSH_SUBSHELL" -eq 0 ]] || return 0
10   # Add ".$SSH_USER" suffix to cache file if $SSH_USER is set and non-empty
11   local cache_file="$ZSH_CACHE_DIR/last-working-dir${SSH_USER:+.$SSH_USER}"
12   pwd >| "$cache_file"
13 }
14
15 # Changes directory to the last working directory
16 lwd() {
17   # Add ".$SSH_USER" suffix to cache file if $SSH_USER is set and non-empty
18   local cache_file="$ZSH_CACHE_DIR/last-working-dir${SSH_USER:+.$SSH_USER}"
19   [[ -r "$cache_file" ]] && cd "$(cat "$cache_file")"
20 }
21
22 # Jump to last directory automatically unless:
23 # - this isn't the first time the plugin is loaded
24 # - it's not in $HOME directory
25 [[ -n "$ZSH_LAST_WORKING_DIRECTORY" ]] && return
26 [[ "$PWD" != "$HOME" ]] && return
27
28 lwd 2>/dev/null && ZSH_LAST_WORKING_DIRECTORY=1 || true