nnn/plugins/fzopen

39 lines
1.1 KiB
Plaintext
Raw Normal View History

2019-01-03 17:35:51 +00:00
#!/usr/bin/env sh
2020-05-06 13:11:01 +00:00
# Description: Fuzzy find a file in directory subtree
2019-08-22 14:28:39 +00:00
# Opens in $VISUAL or $EDITOR if text
# Opens other type of files with xdg-open
2019-01-03 17:35:51 +00:00
#
2020-05-06 13:11:01 +00:00
# Dependencies: fd/find, fzf/skim, xdg-open
2019-12-09 13:06:48 +00:00
#
# Shell: POSIX compliant
2019-01-03 17:35:51 +00:00
# Author: Arun Prakash Jana
2019-12-09 13:06:48 +00:00
if which fzf >/dev/null 2>&1; then
cmd="$FZF_DEFAULT_COMMAND"
2020-04-26 18:35:11 +00:00
if which fd >/dev/null 2>&1; then
[ -z "$cmd" ] && cmd="fd -t f 2>/dev/null"
else
[ -z "$cmd" ] && cmd="find . -type f 2>/dev/null"
fi
entry="$(eval "$cmd" | fzf --delimiter / --nth=-1 --tiebreak=begin --info=hidden)"
# To show only the file name
# entry=$(find . -type f 2>/dev/null | fzf --delimiter / --with-nth=-1 --tiebreak=begin --info=hidden)
elif which sk >/dev/null 2>&1; then
entry=$(find . -type f 2>/dev/null | sk)
2019-12-09 13:06:48 +00:00
else
exit 1
fi
2019-08-22 14:28:39 +00:00
case "$(file -biL "$entry")" in
*text*)
"${VISUAL:-$EDITOR}" "$entry" ;;
*)
if uname | grep -q "Darwin"; then
open "$entry" >/dev/null 2>&1
else
xdg-open "$entry" >/dev/null 2>&1
fi
;;
2019-08-22 14:28:39 +00:00
esac