Ripgrep + peco + vim in Fish shell

Often I search for a file using ripgrep and then open one of the matches in vim. Yesterday I decided to make this process more convenient and went looking for a solution. I found a good one named rg2vim using ripgrep for finding the file and peco for filtering the results but the script published at https://github.com/BurntSushi/ripgrep/issues/304 was for the bash shell.

So here is a fish version which also has the benefit of not opening vim when you ctrl-c out of peco.

function vrg
  set EX_CMD (
        set FL 0
        while read -d : -l fn ln line
            if test $FL = "0"
                printf "e %s" "$fn"
                set FL 1
            else
                printf "|tabnew %s" "$fn"
            end
            printf "|silent :%d" "$ln"
        end < (rg -n "$argv[1]" | peco --select-1 | psub)
    )
    if test -n "$EX_CMD"
      vim -c "$EX_CMD"
    end
end

Leave a Reply

Your email address will not be published. Required fields are marked *