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
functionlet 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
to search for files only by extension/filetype, try this in command line:
with this im searching only for python files
my output:
to exclude a directory from ripgrep
if you want to search
all content from files
at current directory:notice that:
--hidden
is for hidden files-g "\!.git"
is forignoring
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:
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:
well, its quite big, i know. this is the best i've got.
the only differences are:
command_fmt
i added extra args for hidden files and exclusion of git folderofc, you can integrate every above command in vim.
hope these help.