I am using fzf.vim plugin with ripgrep.

Fzf comes with the command :RG and :Rgright out of the box that lets me to search for keywords in files.

However, often I need to search for keywords in specific files. For example: instead of looking for foo anywhere in the Project, I may need to search for foo only in .js files.

I am able to hardcode the file type into the :Rg command. The following code allows me to run :Rg and it will search for keywords only in .js files

function! RgRunner()
  return 'rg --column --line-number --no-heading --color=always --smart-case -g "*.{js}" '

command! -bang -nargs=* Rg
  \ call fzf#vim#grep(RgRunner().shellescape(<q-args>), 1, fzf#vim#with_preview({'options': '--delimiter : --nth 4..'}), <bang>0)

So now when I run :Rg, it searches only in JS files.

fzf js files

And if I change it from *.{js} to *.{rb} ( return 'rg --column --line-number --no-heading --color=always --smart-case -g "*.{rb}" '), :Rg now searches only in .rb files:

fzf rb files

The problem is, these are currently hardcoded. I am having trouble writing the Vim script command that accepts a user input for the glob pattern. I need it so when I run:

  • :Rg '*.{rb}' it searches only in Ruby files
  • :Rg '*.{js}' it searches only in JS files,
  • and so on

How can I change the function/command that I have to accept user-input glob arguments?

fzf command passing glob

That way, when I run :Rg '*.{js}' (or something like that) and fzf.vim lets me to fuzzy-search within the .js file constraint?

  • Have you tried using grepprg instead? I can share more details if you’re interested.
    – D. Ben Knoble
    Commented Jun 13, 2023 at 21:27
  • Yeah, I have greppg set: set grepprg=rg\ --vimgrep\ --smart-case. I can run command like: :grep -g *.rb foo to search for "foo" in ruby files. This is my current means of searching for keywords in specific files - but I am trying to get the FZF fuzzy search experience while searching only in specific files.
    – Iggy
    Commented Jun 14, 2023 at 15:32

1 Answer 1


I would do:

function! RgRunner(...)
  let command = 'rg --column --line-number --no-heading --color=always --smart-case '
  if a:0 >= 1
    let command = command . '-g ' . a:1 . ' '
  return command

command! -bang -nargs=* Rg
  \ call fzf#vim#grep(RgRunner(<f-args>).shellescape(""), 1, fzf#vim#with_preview({'options': '--delimiter : --nth 4..'}), <bang>0)

The RgRunner function takes now a variable number of arguments that are passed through <f-args>.

a:0 is the count of arguments passed.

a:1 is the first argument

  • Hey! Thanks for the reply - It still doesn't work. When I tried to run :Rg *.js, it returns: [Command failed: rg --column --line-number --no-heading --color=always --smart-case -g *.js '*.js']. Vim thinks that the glob argument and the keyword is the same. We just need to figure out a way to separate the arguments. There are basically 2 arguments: The one that's called first with :Rg ARG1, and the second one that's called later after Rg runs (the keyword). RgRunner is a good start though. Maybe I can pick it up from there..
    – Iggy
    Commented Jun 13, 2023 at 18:43
  • It is unclear to me how you want to make the distinction between the file pattern and the keyword you want to search. If you want always to search the current word we can only use the parameter for the file pattern (I have modified the solution accordingly :-) ). Commented Jun 13, 2023 at 19:00
  • Did you tried to use the the -g *.js flag in the normal Fzf? Commented Jun 13, 2023 at 19:01
  • 2
    No, I don't think it'll work. The -g (glob) option is Ripgrep's (github.com/BurntSushi/ripgrep/blob/master/…). Fzf.vim has built-in integration with Ripgrep (github.com/junegunn/fzf.vim#commands, under :Rg), but I don't think the integrated :Rg or :RG command accepts globbing. github.com/junegunn/fzf.vim/blob/master/plugin/fzf.vim#L57
    – Iggy
    Commented Jun 14, 2023 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.