aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/%HOME%/.config/nnn/plugins/myfzcd
blob: 30b5ebeca2efd254fe39c15fd3a74e14d70ddead (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
41
42
43
44
45
46
47
#!/usr/bin/env bash

# Like the fzcd plugin, but better.

set -o errexit -o pipefail
shopt -s inherit_errexit lastpipe

if command -v fzf > /dev/null 2>&1; then
    sel="$( fzf )"
else
    exit 1
fi

nnn_cd() {
    dir="$1"

    if [ -z "$NNN_PIPE" ]; then
        echo "No pipe file found" >&2
        return
    fi

    printf -- '%s' "0c$dir" > "$NNN_PIPE"
}


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

    case "$sel" in
    /*)
        nnn_cd "$sel"
        ;;
    *)  
        sel="${sel#./}"
 
        if [ "$PWD" = "/" ]; then
            nnn_cd "/$sel"
        else
            nnn_cd "$PWD/$sel"
        fi
        ;;
    esac    
fi