aboutsummaryrefslogtreecommitdiffhomepage
path: root/zsh/bd.zsh
diff options
context:
space:
mode:
authorHsieh Chin Fan <typebrook@topo.tw>2023-01-26 22:34:24 +0800
committerHsieh Chin Fan <typebrook@topo.tw>2023-01-26 22:34:24 +0800
commite2a2009b2f5f0e84a8971184d3d1832ba856c194 (patch)
tree36634fea57d5ce07c31b31207c31bfaa22f1f81d /zsh/bd.zsh
parentff0b7fe2d22f7ad93f838bc1fac96642f654dcec (diff)
Update
Diffstat (limited to 'zsh/bd.zsh')
-rw-r--r--zsh/bd.zsh64
1 files changed, 64 insertions, 0 deletions
diff --git a/zsh/bd.zsh b/zsh/bd.zsh
new file mode 100644
index 0000000..33664d6
--- /dev/null
+++ b/zsh/bd.zsh
@@ -0,0 +1,64 @@
1bd () {
2 (($#<1)) && {
3 print -- "usage: $0 <name-of-any-parent-directory>"
4 print -- " $0 <number-of-folders>"
5 return 1
6 } >&2
7 # example:
8 # $PWD == /home/arash/abc ==> $num_folders_we_are_in == 3
9 local num_folders_we_are_in=${#${(ps:/:)${PWD}}}
10 local dest="./"
11
12 # First try to find a folder with matching name (could potentially be a number)
13 # Get parents (in reverse order)
14 local parents
15 local i
16 for i in {$num_folders_we_are_in..2}
17 do
18 parents=($parents "$(echo $PWD | cut -d'/' -f$i)")
19 done
20 parents=($parents "/")
21 # Build dest and 'cd' to it
22 local parent
23 foreach parent (${parents})
24 do
25 dest+="../"
26 if [[ $1 == $parent ]]
27 then
28 cd $dest
29 return 0
30 fi
31 done
32
33 # If the user provided an integer, go up as many times as asked
34 dest="./"
35 if [[ "$1" = <-> ]]
36 then
37 if [[ $1 -gt $num_folders_we_are_in ]]
38 then
39 print -- "bd: Error: Can not go up $1 times (not enough parent directories)"
40 return 1
41 fi
42 for i in {1..$1}
43 do
44 dest+="../"
45 done
46 cd $dest
47 return 0
48 fi
49
50 # If the above methods fail
51 print -- "bd: Error: No parent directory named '$1'"
52 return 1
53}
54_bd () {
55 # Get parents (in reverse order)
56 local num_folders_we_are_in=${#${(ps:/:)${PWD}}}
57 local i
58 for i in {$num_folders_we_are_in..2}
59 do
60 reply=($reply "`echo $PWD | cut -d'/' -f$i`")
61 done
62 reply=($reply "/")
63}
64compctl -V directories -K _bd bd