mirror of
https://github.com/fscotto/infra.git
synced 2026-05-30 23:49:56 +00:00
Add zoxide and custom case-insensitive cd function to bashrc
This commit is contained in:
@@ -26,6 +26,7 @@ common_packages:
|
|||||||
- zip
|
- zip
|
||||||
- vim
|
- vim
|
||||||
- fzf
|
- fzf
|
||||||
|
- zoxide
|
||||||
|
|
||||||
common_dotfiles:
|
common_dotfiles:
|
||||||
- name: .bashrc
|
- name: .bashrc
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ __history_sync() {
|
|||||||
# --- shell options
|
# --- shell options
|
||||||
set -o emacs
|
set -o emacs
|
||||||
shopt -s cmdhist
|
shopt -s cmdhist
|
||||||
shopt -s cdspell
|
|
||||||
|
|
||||||
# --- PATH
|
# --- PATH
|
||||||
[ -d "$HOME/.local/bin" ] && PATH="$HOME/.local/bin:$PATH"
|
[ -d "$HOME/.local/bin" ] && PATH="$HOME/.local/bin:$PATH"
|
||||||
@@ -270,6 +269,14 @@ do
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# =========================
|
||||||
|
# zoxide - smarter cd
|
||||||
|
# =========================
|
||||||
|
|
||||||
|
if command -v zoxide >/dev/null 2>&1; then
|
||||||
|
eval "$(zoxide init bash)"
|
||||||
|
fi
|
||||||
|
|
||||||
# =========================
|
# =========================
|
||||||
# Portable helpers
|
# Portable helpers
|
||||||
# =========================
|
# =========================
|
||||||
@@ -279,6 +286,104 @@ mkcd() {
|
|||||||
mkdir -p -- "$1" && cd -- "$1" || return
|
mkdir -p -- "$1" && cd -- "$1" || return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__cd_resolve_case_insensitive() {
|
||||||
|
local target="$1"
|
||||||
|
local base rest component entry name
|
||||||
|
local -a parts matches
|
||||||
|
|
||||||
|
if [[ "$target" == /* ]]; then
|
||||||
|
base="/"
|
||||||
|
rest=${target#/}
|
||||||
|
else
|
||||||
|
base="."
|
||||||
|
rest=$target
|
||||||
|
fi
|
||||||
|
|
||||||
|
IFS='/' read -r -a parts <<< "$rest"
|
||||||
|
|
||||||
|
for component in "${parts[@]}"; do
|
||||||
|
[ -n "$component" ] || continue
|
||||||
|
|
||||||
|
case "$component" in
|
||||||
|
.|..)
|
||||||
|
base="$base/$component"
|
||||||
|
continue
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
matches=()
|
||||||
|
|
||||||
|
if [[ "$component" == .* ]]; then
|
||||||
|
for entry in "$base"/.*; do
|
||||||
|
[ -d "$entry" ] || continue
|
||||||
|
name=${entry##*/}
|
||||||
|
case "$name" in
|
||||||
|
.|..) continue ;;
|
||||||
|
esac
|
||||||
|
[[ ${name,,} == ${component,,} ]] && matches+=("$entry")
|
||||||
|
done
|
||||||
|
else
|
||||||
|
for entry in "$base"/*; do
|
||||||
|
[ -d "$entry" ] || continue
|
||||||
|
name=${entry##*/}
|
||||||
|
[[ ${name,,} == ${component,,} ]] && matches+=("$entry")
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
case ${#matches[@]} in
|
||||||
|
0)
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
1)
|
||||||
|
base=${matches[0]}
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
printf 'cd: ambiguous case-insensitive match for %s\n' "$target" >&2
|
||||||
|
printf '%s\n' "${matches[@]}" >&2
|
||||||
|
return 2
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
printf '%s\n' "$base"
|
||||||
|
}
|
||||||
|
|
||||||
|
cd() {
|
||||||
|
local resolved status
|
||||||
|
|
||||||
|
if builtin cd "$@" 2>/dev/null; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
status=$?
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]; then
|
||||||
|
builtin cd "$@"
|
||||||
|
return $?
|
||||||
|
fi
|
||||||
|
|
||||||
|
case "$1" in
|
||||||
|
''|-|-*)
|
||||||
|
builtin cd "$@"
|
||||||
|
return $?
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
resolved=$(__cd_resolve_case_insensitive "$1")
|
||||||
|
status=$?
|
||||||
|
|
||||||
|
case $status in
|
||||||
|
0)
|
||||||
|
builtin cd -- "$resolved"
|
||||||
|
;;
|
||||||
|
2)
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
builtin cd "$@"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
extract() {
|
extract() {
|
||||||
[ $# -eq 1 ] || {
|
[ $# -eq 1 ] || {
|
||||||
printf 'usage: extract <archive>\n' >&2
|
printf 'usage: extract <archive>\n' >&2
|
||||||
|
|||||||
Reference in New Issue
Block a user