aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/fzopen
blob: e262d684e323288bc97ef55850cce71cb70c7c55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env sh

# Description: Fuzzy find a file in directory subtree with fzy
#              Opens in $VISUAL or $EDITOR if text
#              Opens other type of files with xdg-open
#
# Requires: fd/find, fzf/fzy/skim, xdg-open
#
# Shell: POSIX compliant
# Author: Arun Prakash Jana

if which fzf >/dev/null 2>&1; then
    cmd="$FZF_DEFAULT_COMMAND"
    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 fzy >/dev/null 2>&1; then
    entry=$(find . -type f 2>/dev/null | fzy)
elif which sk >/dev/null 2>&1; then
	entry=$(find . -type f 2>/dev/null | sk)
else
    exit 1
fi

case "$(file -biL "$entry")" in
    *text*)
        "${VISUAL:-$EDITOR}" "$entry" ;;
    *)
        xdg-open "$entry" >/dev/null 2>&1 ;;
esac