This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/Cd.sh

43 lines
861 B
Bash
Raw Normal View History

# CD (back) to directory.
2023-12-07 04:02:47 +03:00
# Finds first directory that matches the input (case-insensitive).
# Usage: cdd <DIR>
2023-12-07 01:44:42 +03:00
function cdd() {
2023-12-05 21:50:45 +03:00
local target="${1}"
local array
local result
IFS='/' read -r -a array <<< "${PWD}"
array=("${array[@]:1}")
2023-12-05 21:50:45 +03:00
# Make search case-insensitive.
shopt -s nocasematch
2023-12-05 21:50:45 +03:00
# 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
2023-12-05 21:50:45 +03:00
# Clean-up???
shopt -u nocasematch
2023-12-05 21:50:45 +03:00
# Go there!
if [[ "${result}" != "" ]]; then
echo "${result}"
cd "${result}"
else
return 1
fi
}
2023-12-14 22:06:49 +03:00
_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