-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsh-cwd.zsh
67 lines (56 loc) · 893 Bytes
/
zsh-cwd.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/zsh
emulate -L zsh
alias cwd="_cwd::cd"
alias rwd="_cwd::record"
readonly NO_STATE=1
readonly DIR_GONE=2
function _cwd::loc_state
{
echo "${ZSH_CWD_LOCATION:-$HOME/.cwd}"
}
function _cwd::set_state
{
local state="$(_cwd::loc_state)"
echo "$1" > "$state"
}
function _cwd::get_state
{
local loc="$(_cwd::loc_state)"
if [ ! -f $loc ];
then
return $NO_STATE
fi
local wd=$(cat "$loc")
if [ ! -d $wd ];
then
return $DIR_GONE
fi
echo "$wd"
}
function _cwd::record
{
_cwd::set_state "$(pwd)"
}
function _cwd::cd
{
local wd="$(_cwd::get_state)"
if [[ $? ]]
then
cd "$wd"
fi
}
function _cwd::hook
{
# record directory changes
add-zsh-hook chpwd _cwd::record
# change to last known working directory
_cwd::cd
}
function _cwd::unhook
{
chpwd_functions=(${chpwd_functions:#_cwd::record})
}
function _cwd::init
{
_cwd::hook
}