forked from Tarrasch/zsh-bd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bd.zsh
78 lines (74 loc) · 1.57 KB
/
bd.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
68
69
70
71
72
73
74
75
76
77
78
bd () {
# example:
# $PWD == /home/arash/abc ==> $num_folders_we_are_in == 3
local num_folders_we_are_in=${#${(ps:/:)${PWD}}}
local dest="./"
# First try to find a folder with matching name (could potentially be a number)
# Get parents (in reverse order)
local parents
local i
for i in {$num_folders_we_are_in..2}
do
parents=($parents "$(echo $PWD | cut -d'/' -f$i)")
done
parents=($parents "/")
local arg=$1
if (($#<1))
then
if type fzf &> /dev/null
then
local IFS=$'\n'
arg="$(fzf <<< $parents)"
result=$?
unset IFS
if [ $result -gt 0 ]
then
return $result
fi
else
cd ..
return
fi
fi
# Build dest and 'cd' to it
local parent
foreach parent (${parents})
do
dest+="../"
if [[ $parent = *$arg* ]]
then
cd $dest
return 0
fi
done
# If the user provided an integer, go up as many times as asked
dest="./"
if [[ "$arg" = <-> ]]
then
if [[ $arg -gt $num_folders_we_are_in ]]
then
print -- "bd: Error: Can not go up $arg times (not enough parent directories)"
return 1
fi
for i in {1..$arg}
do
dest+="../"
done
cd $dest
return 0
fi
# If the above methods fail
print -- "bd: Error: No parent directory named '$arg'"
return 1
}
_bd () {
# Get parents (in reverse order)
local num_folders_we_are_in=${#${(ps:/:)${PWD}}}
local i
for i in {$num_folders_we_are_in..2}
do
reply=($reply "`echo $PWD | cut -d'/' -f$i`")
done
reply=($reply "/")
}
compctl -V directories -K _bd bd