Converting bash to fish function (use rga-fzf in fish)

645 views Asked by At

I am trying to convert this bash/zsh function into fish. I want to use rga-fzf which is a function for zsh/bash using fzf together with ripgrep-all.

(my default shell is fish)

Here's the zsh/fish function:

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

This is how far I got on my own, and with some help from this post which helps - and this post.

function fif
    set RG_PREFIX rga --files-with-matches
    set -l file
    set file (
        set FZF_DEFAULT_COMMAND $RG_PREFIX "$1" \
            fzf --sort --preview test ! -z {} && rga --pretty --context 5 $argv {} \
                --phony -q "$1" \
                --bind "change:reload:$RG_PREFIX $argv" \
                --preview-window="70%:wrap"
    ) &&
    open "$file"
end

However, now it simply opens the (current) folder and the notification

error: Found argument '--phony' which wasn't expected, or isn't valid in this context

If I change the $1 to $argv then there's no change. Also no change if I change $argv to "$argv"

Any help would be greatly appreciated!

EDIT If I change back the line to :

            fzf --sort --preview "[[ ! -z {} ]] && rga --pretty --context 5 {q} {}" \

Then the error is gone but it only still opens the folder and not fzf

EDIT 2

I've changed back the {q}

so now it reads:

function fif
    set RG_PREFIX rga --files-with-matches
    set -l file
    set file (
        set FZF_DEFAULT_COMMAND $RG_PREFIX "$argv" \
            fzf --sort --preview "[[ ! -z {} ]] && rga --pretty --context 5 {q} {}" \
                --phony -q "$1" \
                --bind "change:reload:$RG_PREFIX {q}" \
                --preview-window="70%:wrap"
    ) &&
    open "$file"
end

The error is gone. But the problem persists, it's not actually opening up fzf.

EDIT 3 I did something wrong with the first set. This shouldn't be

set RG_PREFIX rga --files-with-matches 

but

set -x RG_PREFIX rga --files-with-matches

moreover, everything between the () after the fzf should stay the same so currently I'm here:

function fif
    set -x RG_PREFIX rga --files-with-matches
    set -l file
    set file (
        FZF_DEFAULT_COMMAND="$RG_PREFIX '$1'" \
            fzf --sort --preview="[[ ! -z {} ]] && rga --pretty --context 5 {q} {}" \
                --phony -q "$1" \
                --bind "change:reload:$RG_PREFIX {q}" \
                --preview-window="70%:wrap"
    ) &&
    open "$file"
end

Now it at least opens up fzf but it doesn't actually use rga to filter any of the documents (fzf opens up all the available files in the folder) and the preview window shows:

fish: Unknown command: '[[ ! -z FILENAME ]]' 
fish: 
[[ ! -z 'FILENAME' ]] && rga  --pretty --context 5 '' 'FILENAME'

whereby FILENAME is something like Guidelines 2-2019.pdf

so Mmm one step closer, but not actually using rga and fzf together.

1

There are 1 answers

0
ABeehive On BEST ANSWER
function fif
    set -x RG_PREFIX rga --files-with-matches
    set -l file
    set file (
        FZF_DEFAULT_COMMAND="$RG_PREFIX '$argv'" \
            fzf --sort --preview="[ ! -z {} ] && rga --pretty --context 5 {q} {}" \
                --phony -q "$argv" \
                --bind "change:reload:$RG_PREFIX {q}" \
                --preview-window="70%:wrap"
    ) &&
    open "$file"
end

This is what worked in the end!