Skip to content
phiresky edited this page Jan 15, 2024 · 3 revisions

Important

rga-fzf was added as a native component of rga in 0.10. The following information is outdated.


You can use rga interactively via fzf. Add the following to your ~/.{bash,zsh}rc:

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"
}

And for your ~/.config/fish/config.fish:

function rga-fzf
    set RG_PREFIX 'rga --files-with-matches'
    if test (count $argv) -gt 1
        set RG_PREFIX "$RG_PREFIX $argv[1..-2]"
    end
    set -l file $file
    set file (
        FZF_DEFAULT_COMMAND="$RG_PREFIX '$argv[-1]'" \
        fzf --sort \
            --preview='test ! -z {} && \
                rga --pretty --context 5 {q} {}' \
            --phony -q "$argv[-1]" \
            --bind "change:reload:$RG_PREFIX {q}" \
            --preview-window='50%:wrap'
    ) && \
    echo "opening $file" && \
    open "$file"
end

on Windows

it's possible to do similar job using .bat file:

@echo off
setlocal

set RG_PREFIX=rga --files-with-matches 
set FZF_DEFAULT_COMMAND=%RG_PREFIX% %1 
for /f  "delims=" %%i in ('fzf --sort --preview "rga --pretty --context 5 {q} {}" --phony -q %1 --bind "change:reload:%RG_PREFIX% {q}" --preview-window="70%:wrap"') do (
    set FILE=%%i
)

if not "%FILE%" == "" (
    echo Opening %FILE%
    start "" "%FILE%"
) else (
    echo No file selected.
)

endlocal

put this script to rga-fzf.bat somewhere in your %PATH%

Clone this wiki locally