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
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
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 .
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:
command_fmt
i added extra args for hidden files and exclusion of git folder'--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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With