blob: 2bc70f184c6ab2cd95e7a12ddac669ad9778dccd (
plain) (
blame)
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
|
# Move back up the directory tree to the first directory matching the name
bd() {
# If there are no arguments, we just move up one directory (cd ..)
if [[ $1 ]] ; then
dir="${PWD%/$1*}"/"$1"
else
dir=..
fi
# Check the directory exists and try to cd into it if possible
if [[ -d $dir ]] ; then
builtin cd -- "$dir"
else
printf 'bash: %s: No dir found in PWD named %s\n' \
"$FUNCNAME" "$1" >&2
return 1
fi
}
# Completion setup for bd
_bd() {
local word=${COMP_WORDS[COMP_CWORD]}
# Build a list of dirs in $PWD
local -a dirs
while read -d / -r dir ; do
if [[ $dir ]] ; then
dirs=("${dirs[@]}" "$dir")
fi
done < <(printf %s "$PWD")
# Complete with matching dirs
COMPREPLY=( $(compgen -W "${dirs[*]}" -- "$word") )
}
complete -F _bd bd
|