43 lines
861 B
Bash
43 lines
861 B
Bash
# CD (back) to directory.
|
|
# Finds first directory that matches the input (case-insensitive).
|
|
# Usage: cdd <DIR>
|
|
function cdd() {
|
|
local target="${1}"
|
|
local array
|
|
local result
|
|
IFS='/' read -r -a array <<< "${PWD}"
|
|
array=("${array[@]:1}")
|
|
|
|
# Make search case-insensitive.
|
|
shopt -s nocasematch
|
|
|
|
# Find desired dir.
|
|
local found=1
|
|
for (( idx=${#array[@]}-2 ; idx>=0 ; idx-- )); do
|
|
dir="${array[idx]}"
|
|
[[ "${dir}" =~ "${target}" ]] && found=0
|
|
[[ ${found} = 0 ]] && result="/${dir}${result}"
|
|
done
|
|
|
|
# Clean-up???
|
|
shopt -u nocasematch
|
|
|
|
# Go there!
|
|
if [[ "${result}" != "" ]]; then
|
|
echo "${result}"
|
|
cd "${result}"
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
_cdd_directories() {
|
|
local array
|
|
IFS='/' read -r -a array <<< "${PWD}"
|
|
array=("${array[@]:1}")
|
|
unset array[-1]
|
|
_autocomplete_first "${array[@]}"
|
|
}
|
|
|
|
complete -o nosort -o filenames -F _cdd_directories cdd
|