2022-01-10 10:49:56 +00:00
|
|
|
# Utilities for file system navigation
|
2021-09-18 21:04:50 +00:00
|
|
|
|
2022-10-13 19:56:23 +00:00
|
|
|
alias watchdir='watch -c -n 1 exa -l --icons'
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
# Useful when the current directory was recreated
|
2021-12-12 19:24:56 +00:00
|
|
|
alias recd='cd $PWD'
|
2022-03-15 16:24:43 +00:00
|
|
|
# [c]hange [d]irectory [e]xisting - cd into the first existing directory
|
|
|
|
cde() {
|
|
|
|
local _dir="$1"
|
|
|
|
while ! test -d "$_dir"
|
|
|
|
do _dir="$(dirname "$_dir")"
|
|
|
|
done
|
|
|
|
cd "$_dir" "${@:2}"
|
|
|
|
}
|
2021-09-18 21:04:50 +00:00
|
|
|
|
|
|
|
# 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
|
2021-09-18 21:04:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Switch directory & ls
|
|
|
|
cl() {
|
2021-11-25 12:57:02 +00:00
|
|
|
builtin cd $1
|
|
|
|
ls --almost-all --group-directories-first --file-type
|
2021-09-18 21:04:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 16:24:43 +00:00
|
|
|
# cd but search for data dirs and run ls after switching to target dir
|
2021-09-18 21:04:50 +00:00
|
|
|
cd() {
|
2021-11-25 12:57:02 +00:00
|
|
|
if test -d "$DATA" && test "${1:--}" != "-" -a ! -d "$1" -a $# -eq 1
|
|
|
|
then
|
2022-05-03 07:38:55 +00:00
|
|
|
dir=$(fd --no-ignore --glob "$1*" "$DATA" --maxdepth 2 --type d --max-results 1 2>/dev/null)
|
2021-11-25 12:57:02 +00:00
|
|
|
test -n "$dir" && cd "$dir"
|
|
|
|
else
|
2021-12-11 01:01:01 +00:00
|
|
|
builtin cd "$@" &&
|
2022-04-10 09:37:26 +00:00
|
|
|
# we have to use two ls commands here, since:
|
|
|
|
# - a single ls command hangs on multi-column formats when combined with color
|
|
|
|
# - column malaligns output with ANSI escape sequences (view with LESS="" less)
|
|
|
|
# column --fillrows --output-width $(expr $COLUMNS + 20)
|
|
|
|
# pr -4 -l1 -W $COLUMNS -S" "
|
|
|
|
command ls --sort=none --quote-name |
|
|
|
|
head -12 |
|
2022-06-27 11:03:11 +00:00
|
|
|
xargs ls --sort=time --format=vertical --directory --literal --classify --color=always --width $COLUMNS |
|
2021-11-25 12:57:02 +00:00
|
|
|
head -3
|
|
|
|
code=$? && test $code -eq 141 || return $code
|
|
|
|
fi
|
2021-09-18 21:04:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 10:49:56 +00:00
|
|
|
# LOCATE {{{1
|
2022-04-11 06:31:13 +00:00
|
|
|
# TODO no double heading with bat
|
2022-07-18 10:09:29 +00:00
|
|
|
alias fselect='fzf -0 -1 --reverse --height=40% --keep-right |
|
|
|
|
while read f; do test -d "$f" && cd "$f" || { highlight "$f" && print -s b \"$f\" && b "$f"; }; done';
|
2021-11-07 05:00:29 +00:00
|
|
|
loci() {
|
2021-11-25 12:57:02 +00:00
|
|
|
locate --all --ignore-case --basename --existing "$@" |
|
2022-04-04 10:26:13 +00:00
|
|
|
command grep --extended-regexp --ignore-case --color=always $(echo "$|${@:$#}" | sed 's/ /|/g') |
|
|
|
|
fselect
|
2021-11-07 05:00:29 +00:00
|
|
|
}
|
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
|
2022-08-04 21:55:07 +00:00
|
|
|
alias loca='noglob sudo updatedb -l 0 --prunenames "" --prunefs "tmpfs sysfs debugfs" --prunepaths "" -o /var/cache/locate-all.db &&
|
|
|
|
sudo chmod +r /var/cache/locate-all.db &&
|
|
|
|
loci --database /var/cache/locate-all.db'
|
2022-09-27 20:04:32 +00:00
|
|
|
hunta() {
|
|
|
|
unbuffer hunt -h "$@" / | less -RF +G
|
|
|
|
}
|
2021-10-15 12:31:08 +00:00
|
|
|
|
2022-01-10 10:49:56 +00:00
|
|
|
# ZOXIDE {{{1
|
2021-09-18 21:04:50 +00:00
|
|
|
alias c=z
|
2022-01-10 10:49:56 +00:00
|
|
|
# Listing for quick directory switcher based on zoxide and fzf
|
|
|
|
alias zoxide-list='noglob zoxide query -sl'
|
2022-05-31 23:04:05 +00:00
|
|
|
__zx_ls="ls --almost-all --color --human-readable --group-directories-first --file-type"
|
2022-11-09 17:52:27 +00:00
|
|
|
alias fzf-dir="fzf -0 -1 --tiebreak=chunk,end,length \
|
2022-05-31 23:04:05 +00:00
|
|
|
--preview-window=30% --preview='$__zx_ls {}' \
|
|
|
|
--height=80% --reverse --keep-right"
|
|
|
|
# fzf for zoxide - returns the selected path stripped of its score
|
2022-01-10 10:49:56 +00:00
|
|
|
zoxide-fzf() {
|
2022-05-31 23:04:05 +00:00
|
|
|
fzf-dir -n2.. --preview="$__zx_ls {2..}" "$@" | sed 's|.* /|/|'
|
2022-01-10 10:49:56 +00:00
|
|
|
}
|
|
|
|
# Locate directories and add 0 zoxide score
|
|
|
|
locate-zox() {
|
2022-06-27 21:01:11 +00:00
|
|
|
locate --basename --ignore-case "$@" |
|
2022-06-27 20:58:12 +00:00
|
|
|
# TODO too slow
|
|
|
|
# while read -r file; do test -d "$file" && echo "$file"; done |
|
2022-01-10 10:49:56 +00:00
|
|
|
sed 's/^/ 0 /'
|
|
|
|
}
|
|
|
|
# Switch directory heuristically using zoxide and locate
|
2021-09-18 21:04:50 +00:00
|
|
|
d() {
|
2022-04-28 11:02:26 +00:00
|
|
|
test "$1" != "-" -a ! -d "$1" -a $# -eq 1 || { cd "$@" && return; }
|
2022-01-10 10:49:56 +00:00
|
|
|
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)
|
2022-08-04 08:52:16 +00:00
|
|
|
# which is not the current directory
|
2021-11-25 12:57:02 +00:00
|
|
|
# If not select with fzf, using locate to find extra options
|
2022-05-31 23:04:05 +00:00
|
|
|
target="$(if test -n "$query" &&
|
2022-01-10 20:39:57 +00:00
|
|
|
expr "$(echo "$query" | sed 's| \+\([^ ]\+\).*|\1|;q')" \> 20 \& \
|
2022-08-04 08:52:16 +00:00
|
|
|
$(echo "$query" | sed 'N;s|/.* \([0-9]\+\)|> 10 * \1|;q' | sed 's| */.*||') >/dev/null &&
|
|
|
|
[[ ! "$(echo "$query" | head -1)" =~ ".*$PWD" ]]
|
2021-11-25 12:57:02 +00:00
|
|
|
then echo "$query" | head -1
|
2022-01-10 10:49:56 +00:00
|
|
|
else test -n "$query" && echo "$query"; locate-zox "$@"
|
2022-05-31 23:30:29 +00:00
|
|
|
fi | zoxide-fzf)"
|
|
|
|
test $? -lt 2 && test -z "$target" && sudo updatedb && target="$(locate --basename "$@" | fzf-dir)"
|
2022-06-27 20:58:12 +00:00
|
|
|
test -n "$target" && cde "$target"
|
2021-09-18 21:04:50 +00:00
|
|
|
}
|
2022-01-10 10:49:56 +00:00
|
|
|
# Switch directory interactively using zoxide and locate
|
2021-09-18 21:04:50 +00:00
|
|
|
di() {
|
2022-01-10 10:49:56 +00:00
|
|
|
test "$1" = "-" || local dir=$({ zoxide-list "$@"; locate-zox "$@" } | zoxide-fzf)
|
2021-11-25 12:57:02 +00:00
|
|
|
cd "${dir:-$1}"
|
2021-09-18 21:04:50 +00:00
|
|
|
}
|