Ripgrep is a command line tool like grep. It was written in recent years, is faster and has a number of features.

Note that ripgrep will recurse through subdirectories by default. So you may focus on narrowing the list of files it searches rather than expanding it like in grep.

Search for function only in org files, also with case insensitive version

rg -g '*.org' function
rg -g '*.org' -i function

Search for lines that don’t have the text within a file

rg -v function README.md

Search for function, but not under any node_modules subdirs

rg -g '!node_modules' function

Show number of lines before and after match. If you want the same number before and after, then use single context command.

rg -B 3 -A 2 function
rg -C 3 function

Search ruby type for method definitions

rg -truby def

List known file types and filter by text or regular expression

rg --type-list
rg --type-list | rg js
rg --type-list | rg '^js'
rg --type-list | rg js | rg -v '^js'

Display filename only for files with match

rg -l foo

Display filename and number of matched lines

rg -c foo

Display filename and number of pattern matches (not lines)

rg --count-matches foo

Search within dir but exclude some files

rg -g 'grep-matcher/src/*' -g '!interpolate.*' foo

Troubleshooting

What files is it searching?

Ripgrep likes to use file types, which represents a collection of file extensions that it will search given a single type.

For example, the ruby type is defined to be the following files. So by specifying one type, you can easily search many different related files.

  • *.gemspec
  • *.rb
  • .irbrc
  • Gemfile
  • Rakefile

Try these two searches with a common term to see the difference

rg -truby --count-matches def
rg --count-matches def

Resources