Ripgrep Tips and Tricks

Image of Author
March 10, 2022 (last updated December 20, 2023)

Ripgrep is a fast search tool. The command line syntax is rg [expression] [path]. There is an introductory guide in the repo.

Regex Syntax

The expression is a Rust Regex. To be safe you should wrap the expression in quotes, ' or ", though for simple queries you can omit them. For example, if you are searching for content containing ^[ in zsh:

rg ^[ ./
# -> zsh: bad pattern: ^[

rg '^[' ./
# -> regex parse error: ...

rg \^\[ ./
# -> regex parse error: ...

rg '\^\[' ./
# -> success

Flags

  • --no-ignore will ignore things like .gitignore which are respected by default in ripgrep.

For example, I will occassionaly want to do a quick search for a pattern in a JS repo inclusive of the node_modules folder. rg --no-ignore PATTERN ./ will do that.

  • -C or --context show lines before and after each match

For example, rg -C 10, will show 10 lines before and after a line match. This is the same as writing -B 10 for before-context and -A 10 for after-context.

  • -l or --files-with-matches only show file paths

    Print the paths with at least one match and suppress match contents. This overrides --files-without-match.

  • --no-messages suppress errors

    Suppress all error messages related to opening and reading files. Error messages related to the syntax of the pattern given are still shown. This flag can be disabled with the --messages flag.

Commands

  • rg -l --no-messages [query] [path] will give you a simple list of file paths whose contents contain the query.

    One cool thing you can do with this command is open files in a text editor. For example, to open the file containing the function doSomething:

    vim $(rg -l 'function doSomething\(' ./)
    

Pipe Syntax

brew list | rg "curl"