1

I have the following in my .vimrc which (I believe) makes :grep within Vim use rg:

if executable('rg')
  set grepprg=rg\ --no-heading\ --vimgrep\ --hidden\ --case-sensitive\ --ignore-vcs\ --glob\ '!.git'\ --glob\ '!node_modules'
endif

I want to search for all definitions of functions named render.... If I do

rg -e \(const\|let\)\ render .

on the command line, I get what I'm looking for.

But

:grep -e \(const\|let\)\ render

in vim results in

zsh:1: command not found: let) render
regex parse error:
    (const
    ^
error: unclosed group

I've tried some other combos of \, putting the whole query in /.../, can't quite get it working.

How do I use the alternation operator in ripgrep in vim?

2
  • 1
    Crosspost from SO
    – cafce25
    Commented Jan 25, 2023 at 9:14
  • Shell is seeing your parens for itself and then creates a subshell. It need to be a string passed to rg
    – gildux
    Commented Jan 25, 2023 at 9:14

1 Answer 1

2

I would not escape neither the parenthesis and I would add quotes:

grep "(const\|let) render"

When using grepprg Vim pass the arguments to the program mostly unchanged. The only exception of the pipe character (|) that should be escaped (\|).

The syntax of :vimgrep and !rg are slightly different:

  • Where :vimgrep expect the pattern to be / enclosed !rg expect it to be enclosed by quotes (")
  • Where :vimgrep uses Vim regular expression !rg uses Rust regular expressions:
    • :vimgrep interprets ( literally; !rg interprets it as grouping (equivalent to \( in :vimgrep)
    • :vimgrep match \< literally as word start; !rg match \b as word start
    • ...
  • ...

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.