summary refs log tree commit diff
path: root/dot_local/share/zsh/fzf/fzf.plugin.zsh
blob: 70f73b90aea9233c7c7848d452c95377f6ee0668 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
ftop() {
    FZF_DEFAULT_COMMAND='ps -ef' \
      fzf --bind 'ctrl-r:reload(ps -ef)' \
          --header 'Press CTRL-R to reload' \
          --header-lines=1 \
          --height=50% --layout=reverse
}

__fzf-jump-widget() {
    LBUFFER="${LBUFFER}$(fasd -ld | tac | fzf)"
    local ret=$?
    zle redisplay
    if [[ $ret == 0 ]]; then
        zle accept-line
    fi
    return $ret
}

zle -N __fzf-jump-widget

__fzf-open-widget() {
    FILE=$(fasd -lf | tac | fzf)
    local ret=$?
    if [[ $ret != 0 ]]; then
        return $ret
    fi
    if [[ "${LBUFFER}" != "" ]]; then
        LBUFFER="${LBUFFER}${FILE}"
    elif [[ $(file --mime ${FILE}) =~ "binary" ]]; then
        LBUFFER="xdg-open ${FILE}"
    else
        LBUFFER="${EDITOR} ${FILE}"
    fi
    zle redisplay
    if [[ $ret == 0 ]]; then
        zle accept-line
    fi
    return $ret
}

zle -N __fzf-open-widget

# Search files with preview
__fzf-preview-widget() {
    LBUFFER="${LBUFFER}$(ls | fzf -e --preview=${FZF_BAT_PREVIEW})"
}
zle -N __fzf-preview-widget

# rg on Asteroids
rf() {
    RG_PREFIX="rga --files-with-matches"
    local file
    file="$(
        FZF_DEFAULT_COMMAND="$RG_PREFIX '$1'" \
            fzf --sort --preview="[[ ! -z {} ]] && echo {} && rga --pretty --context 5 {q} {}" \
                --phony -q "$1" \
                --bind "change:reload:$RG_PREFIX {q}" \
                --preview-window="70%:wrap"
    )" &&
    echo $file
    # echo "opening $file" &&
    # xdg-open "$file"
}

# Fancy git diff using fzf
if which git &>/dev/null; then
    alias gdf='
        git status -s \
         | fzf --no-sort --reverse \
         --preview "git diff --color=always {+2} | delta" \
         --bind=ctrl-j:preview-down --bind=ctrl-k:preview-up \
         --preview-window=right:70%:wrap'
fi

# figure out some useful things to do...
# https://junegunn.kr/2016/07/fzf-git/
_FZF_BAT_PREVIEW='[[ $(file --mime {}) =~ "binary" ]] && echo "<folder/binary file>" || bat --style=numbers --color=always {} --line-range :500'
alias fzfp="fzf --preview='${_FZF_BAT_PREVIEW}'"
alias fzfd="fzf --preview='${_FZF_BAT_PREVIEW}' --bind='enter:execute(bat --style=numbers --color=always --paging=always {}),ctrl-e:execute(micro {}),tab:toggle-preview'"

export FZF_DEFAULT_OPTS="-1 --height 50% --layout=reverse --border --multi --info=inline"

# Use fd/rg/ag if possible to follow any ignores / sensible configs
if which fd &>/dev/null; then
    export FZF_DEFAULT_COMMAND='fd --type f'
elif which rg &>/dev/null; then
    export FZF_DEFAULT_COMMAND='rg --files'
elif which ag &>/dev/null; then
    export FZF_DEFAULT_COMMAND='ag --files-with-matches'
fi