aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/fzcd
blob: 5e0b5f0958c6e8a9404af3af6aa1eabfa5da1efc (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
36
37
38
39
40
#!/usr/bin/env sh

# Description: Run fzf/fzy, fd/fdfind/find and go to the directory of the file selected
#
# Shell: POSIX compliant
# Author: Anna Arad

. "$(dirname "$0")"/.nnn-plugin-helper

if [ "$(cmd_exists fzy)" -eq "0" ]; then
	if [ "$(cmd_exists fd)" -eq "0" ]; then
		fd=fd
	elif [ "$(cmd_exists fdfind)" -eq "0" ]; then
		fd=fdfind
	else
		fd=find
	fi

	sel=$($fd | fzy)
elif [ "$(cmd_exists fzf)" -eq "0" ]; then
	sel=$(fzf)
else
	exit 1
fi

if [ -n "$sel" ]; then
	if ! [ -d "$sel" ]; then
		sel=$(dirname "$sel")
    elif [ "$sel" = "." ]; then
        exit 0
	fi

    # Remove "./" prefix if it exists
    sel="${sel#./}"
    if [ "$PWD" = "/" ]; then
	    nnn_cd "/$sel"
    else
	    nnn_cd "$PWD/$sel"
    fi
fi