2021-09-18 21:04:50 +00:00
|
|
|
# Utilities regarding file system navigation
|
|
|
|
|
|
|
|
# Useful when the current directory was recreated
|
|
|
|
alias rcd="cd $PWD"
|
|
|
|
# cd into the parent if arg is a file
|
|
|
|
cdd() { cd "$@" 2>/dev/null || cd "$(dirname "$1")" "${@:2}" }
|
|
|
|
|
|
|
|
# Go up a number of dirs
|
|
|
|
up() {
|
2021-09-21 20:32:35 +00:00
|
|
|
if [[ $# < 1 ]] ; then
|
|
|
|
cd ..
|
|
|
|
else
|
|
|
|
CDSTR=""
|
|
|
|
for i in {1..$1} ; do
|
|
|
|
CDSTR="../$CDSTR"
|
|
|
|
done
|
|
|
|
cd $CDSTR
|
|
|
|
fi
|
2021-09-18 21:04:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Switch directory & ls
|
|
|
|
cl() {
|
2021-09-21 20:32:35 +00:00
|
|
|
builtin cd $1
|
|
|
|
ls --almost-all --group-directories-first --file-type
|
2021-09-18 21:04:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 12:31:08 +00:00
|
|
|
# cd but search for data dirs and ls in new dir
|
2021-09-18 21:04:50 +00:00
|
|
|
cd() {
|
2021-09-24 18:20:29 +00:00
|
|
|
if test -d "$DATA" && test "$1" != "-" -a ! -d "$1" -a $# -eq 1
|
|
|
|
then
|
|
|
|
dir=$(fd --no-ignore --glob "$1*" "$DATA" --maxdepth 2 --type d --max-results 1)
|
|
|
|
test -n "$dir" && cd "$dir"
|
|
|
|
else
|
|
|
|
builtin cd $1 &&
|
2021-11-07 05:00:29 +00:00
|
|
|
command ls --literal --file-type --color=always --format=vertical -U -w $COLUMNS |
|
|
|
|
#pr -4 -l1 -W $COLUMNS -S" " |
|
|
|
|
head -3
|
|
|
|
code=$? && test $code -eq 141 || return $code
|
2021-09-24 18:20:29 +00:00
|
|
|
fi
|
2021-09-18 21:04:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 12:31:08 +00:00
|
|
|
# LOCATE
|
2021-11-09 04:37:07 +00:00
|
|
|
alias fselect='fzf -0 -1 --reverse --height=30% | while read f; do test -d "$f" && cd "$f" || b "$f"; done'
|
2021-11-07 05:00:29 +00:00
|
|
|
loci() {
|
|
|
|
locate --all --ignore-case --basename --existing "$@" |
|
|
|
|
command grep --extended-regexp --ignore-case --color=always $(echo "$|${@:$#}" | sed 's/ /|/g') | fselect
|
|
|
|
}
|
2021-10-15 12:31:08 +00:00
|
|
|
alias loc='noglob loci'
|
|
|
|
alias uloc='noglob sudo updatedb && loci'
|
|
|
|
# locate exactly
|
2021-10-17 11:41:29 +00:00
|
|
|
locei() { locate --all --basename "\\$1" "$@" | fselect }
|
2021-10-15 12:31:08 +00:00
|
|
|
alias loce='noglob locei'
|
|
|
|
# locate all
|
2021-10-31 18:28:46 +00:00
|
|
|
alias loca='noglob sudo updatedb --prunenames "" -o /var/lib/mlocate/all.db && loci --database /var/lib/mlocate/all.db'
|
2021-10-15 12:31:08 +00:00
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
# ZOXIDE
|
|
|
|
alias c=z
|
|
|
|
d() {
|
|
|
|
test "$1" != "-" -a ! -d "$1" -a $# -eq 1 || { cd "$@" && return }
|
|
|
|
local query="$(zf "$@")"
|
|
|
|
# First find out whether there is an obvious match
|
|
|
|
# (score at least ten times above runner-up and score above 20)
|
|
|
|
# If not select with fzf, using locate to find extra options
|
2021-10-13 17:15:26 +00:00
|
|
|
cd "$(if test -n "$query" && expr "$(echo "$query" | head -1 | cut -d' ' -f1)" \> 20 \& $(echo "$query" | sed 'N;s|/.* \([0-9]\)|> 10 * \1|;q' | sed 's| */.*||') >/dev/null #2>&1
|
2021-09-18 21:04:50 +00:00
|
|
|
then echo "$query" | head -1
|
2021-10-31 18:28:46 +00:00
|
|
|
else test -n "$query" && echo "$query"; locz "$@"
|
2021-09-18 21:04:50 +00:00
|
|
|
fi | zfz)"
|
|
|
|
}
|
|
|
|
di() {
|
2021-09-24 18:20:29 +00:00
|
|
|
test $# -eq 1 && test "$1" = "-" -o -d "$1" || local dir=$({ zf "$@"; locz "$@" } | zfz)
|
2021-10-11 08:02:14 +00:00
|
|
|
cd "${dir:-$1}"
|
2021-09-18 21:04:50 +00:00
|
|
|
}
|