dotfiles/.config/shell/browse

93 lines
2.9 KiB
Plaintext
Raw Normal View History

# Utilities for file system navigation
# Useful when the current directory was recreated
alias recd='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-11-25 12:57:02 +00:00
if [[ $# < 1 ]] ; then
cd ..
else
CDSTR=""
for i in {1..$1} ; do
CDSTR="../$CDSTR"
done
cd $CDSTR
fi
}
# Switch directory & ls
cl() {
2021-11-25 12:57:02 +00:00
builtin cd $1
ls --almost-all --group-directories-first --file-type
}
# cd but search for data dirs and ls in new dir
cd() {
2021-11-25 12:57:02 +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 "$@" &&
2021-11-25 12:57:02 +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
fi
}
# LOCATE {{{1
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'
loci() {
2021-11-25 12:57:02 +00:00
locate --all --ignore-case --basename --existing "$@" |
command grep --extended-regexp --ignore-case --color=always $(echo "$|${@:$#}" | sed 's/ /|/g') | fselect
}
alias loc='noglob loci'
alias uloc='noglob sudo updatedb && loci'
# locate exactly
locei() { locate --all --basename "\\$1" "$@" | fselect }
alias loce='noglob locei'
# locate all
2022-01-17 20:25:24 +00:00
alias loca='noglob sudo updatedb --prunenames "" --prunefs "" --prunepaths "/var/cache" -o /var/cache/locate-all.db && loci --database /var/cache/locate-all.db'
# ZOXIDE {{{1
alias c=z
# Listing for quick directory switcher based on zoxide and fzf
alias zoxide-list='noglob zoxide query -sl'
# fzf for zoxide
zoxide-fzf() {
# returns the selected path stripped of its score
fzf -0 -1 -n2.. --tiebreak=index \
--preview-window=20% --preview="ls -a --color --human-readable --group-directories-first --file-type {2..}" \
--height=80% --reverse |
sed 's|.* /|/|'
}
# Locate directories and add 0 zoxide score
locate-zox() {
locate --basename --ignore-case "$@" |
while read -r file; do test -d "$file" && echo "$file"; done |
sed 's/^/ 0 /'
}
# Switch directory heuristically using zoxide and locate
d() {
2021-11-25 12:57:02 +00:00
test "$1" != "-" -a ! -d "$1" -a $# -eq 1 || { cd "$@" && return }
local query="$(zoxide-list "$@")"
2021-11-25 12:57:02 +00:00
# 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
cd "$(if test -n "$query" &&
2022-01-10 20:39:57 +00:00
expr "$(echo "$query" | sed 's| \+\([^ ]\+\).*|\1|;q')" \> 20 \& \
2021-11-25 12:57:02 +00:00
$(echo "$query" | sed 'N;s|/.* \([0-9]\)|> 10 * \1|;q' | sed 's| */.*||') >/dev/null #2>&1
then echo "$query" | head -1
else test -n "$query" && echo "$query"; locate-zox "$@"
fi | zoxide-fzf)"
}
# Switch directory interactively using zoxide and locate
di() {
test "$1" = "-" || local dir=$({ zoxide-list "$@"; locate-zox "$@" } | zoxide-fzf)
2021-11-25 12:57:02 +00:00
cd "${dir:-$1}"
}