Skip to content

Instantly share code, notes, and snippets.

@NthPortal
Last active October 9, 2019 03:24
Show Gist options
  • Save NthPortal/8cf194ec3579f7c7c41c88f0605b3041 to your computer and use it in GitHub Desktop.
Save NthPortal/8cf194ec3579f7c7c41c88f0605b3041 to your computer and use it in GitHub Desktop.
Recursively resolving `which` utilities
_rwhich_1() {
readlink -e "$(command -v "$1")"
}
_rwhich_comp() {
mapfile -t COMPREPLY < <(compgen -c "${COMP_WORDS[COMP_CWORD]}")
return 0
}
# recursively resolve commands ("recursive `which`")
rwhich() {
for cmd in "$@" ; do
_rwhich_1 "$cmd"
done
}
complete -F _rwhich_comp rwhich
# recursively resolve commands and run them through `ls -alF`
lwhich() {
for cmd in "$@" ; do
local resolved="$(_rwhich_1 "$cmd")"
if [ -n "$resolved" ] ; then
ls --color=auto -alF "$resolved"
fi
done
}
complete -F _rwhich_comp lwhich
# recursively resolve commands and open them in a pager
pwhich() {
local files=()
mapfile -t files < <(rwhich "$@")
if [ ${#files[@]} -gt 0 ] ; then
"${PAGER:-pager}" "${files[@]}"
fi
}
complete -F _rwhich_comp pwhich
# recursively resolve commands and open them in an editor
ewhich() {
local files=()
mapfile -t files < <(rwhich "$@")
if [ ${#files[@]} -gt 0 ] ; then
"${VISUAL:-${EDITOR:-editor}}" "${files[@]}"
fi
}
complete -F _rwhich_comp ewhich
# recursively resolve commands and concatenate their files
cwhich() {
local files=()
mapfile -t files < <(rwhich "$@")
if [ ${#files[@]} -gt 0 ] ; then
cat "${files[@]}"
fi
}
complete -F _rwhich_comp cwhich
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment