Using Ripgrep with flags in fzf.vim

2k views Asked by At

I am using function below to search content in files with ripgrep using fzf in vim

function! RipgrepFzf(query, fullscreen)
  let command_fmt = 'rg --column --line-number --no-heading --color=always --smart-case -- %s || true'
  let initial_command = printf(command_fmt, shellescape(a:query))
  let reload_command = printf(command_fmt, '{q}')
  let spec = {'options': ['--phony', '--query', a:query, '--bind', 'change:reload:'.reload_command]}
  call fzf#vim#grep(initial_command, 1, fzf#vim#with_preview(spec), a:fullscreen)
endfunction

command! -nargs=* -bang FRG call RipgrepFzf(<q-args>, <bang>0)

I am able to perform a simple search with it, However i am not able to use RG flags to make my search more precise (search in filetype, exclude directory, or search in sub-directory etc.)

I found some articles suggest small changes in function to get what i am looking for are below:

  • Remove -- before %s

    let command_fmt = 'rg --column --line-number --no-heading --color=always --smart-case %s || true'
    
  • Remove shellescape function

    let initial_command = printf(command_fmt, a:query)
    

After these changes i am able to use flags initially, However while updating my search it's not working as expected.

I would like to use RG as it is (same way it works in terminal) within fzf in vim

2

There are 2 answers

0
alexzander On

to search for files only by extension/filetype, try this in command line:

rg . --files -g "*.{py}"

with this im searching only for python files

my output:

> rg . --files -g "*.{py}"
./another.py
./colors_do_not_apply.py

to exclude a directory from ripgrep

if you want to search all content from files at current directory:

> rg --column --line-number --no-heading --color=always --smart-case --hidden -g "\!.git" .

notice that:

  • --hidden is for hidden files
  • -g "\!.git" is for ignoring git folder (you can do the same with others)

note:

i used "\!.git", that is only used in terminal (my zsh explodes without ! escaped), in vim you can use it as "!.git"

if you want to search only for files and exlcude git folder at current dir:

> rg --hidden -g "\!.git" --files .

search in sub-directory is made automatically so you dont need to worry

finally

i saw you copy pasted a fancy function from fzf.vim on github, i use that too :)

here is my function:

" search for content in files in current project, including hidden ones and ignoring git
function! SearchContentInFiles(query, fullscreen)
  let command_fmt = 'rg --column --line-number --no-heading --color=always --smart-case --hidden -g "!.git" -- %s || true'
  let initial_command = printf(command_fmt, shellescape(a:query))
  let reload_command = printf(command_fmt, '{q}')
  let spec = {'options': ['--phony', '--query', a:query, '--bind', 'change:reload:'.reload_command, '--exact', '--pointer=@', '-i', '-m', '--cycle', '--border=none', '--marker=*', '--ansi', '--preview-window=left,50%', '--bind=alt-bspace:backward-kill-word,ctrl-x:beginning-of-line+kill-line,ctrl-a:select-all', '--color=16,fg+:bright-red,hl:bright-blue,hl+:green,query:blue,prompt:yellow,info:magenta,pointer:bright-yellow,marker:bright-blue,spinner:bright-blue,header:blue', '--prompt=content from files(+hidden) (git ignored) at . > ']}
  call fzf#vim#grep(initial_command, 1, fzf#vim#with_preview(spec), a:fullscreen)
endfunction

command! -nargs=* -bang SCIF call SearchContentInFiles(<q-args>, <bang>0)
nnoremap <silent> <S-F> :SCIF!<CR>

well, its quite big, i know. this is the best i've got.

the only differences are:

  • in command_fmt i added extra args for hidden files and exclusion of git folder
  • the name of the function
  • fzf options (there are a lot of options), more exactly these options:
'--exact' - exact match
'--pointer=@', 
'-i', 
'-m', 
'--cycle', (repeat after end)
'--border=none', 
'--marker=*', 
'--ansi', (enable colors)
'--preview-window=left,50%', 
'--bind=alt-bspace:backward-kill-word,ctrl-x:beginning-of-line+kill-line,ctrl-a:select-all', (some keybindings)
'--color=16,fg+:bright-red,hl:bright-blue,hl+:green,query:blue,prompt:yellow,info:magenta,pointer:bright-yellow,marker:bright-blue,spinner:bright-blue,header:blue', (color scheme)
'--prompt=content from files(+hidden) (git ignored) at . > ' (prompt text)

ofc, you can integrate every above command in vim.

hope these help.

0
Burak Kaymakci On

First, you need to remove the -- from the comand string because

a double dash (--) is used in most Bash built-in commands and many other commands to signify the end of command options, after which only positional arguments are accepted.original answer

And the second is, you're interested in reload_command because that's what's called whenever you change the string on preview window.

If you change your reload_command to be

let reload_command = printf(command_fmt, shellescape('{q}'))

then you should be able to pass flags on the preview window.

I wanted to see how fzf calls that reload command just to see the difference shellescape makes but I think that would take some time for me as I am not really familiar. If I do that I'll edit my answer as well.