ripgrep
is a command line tool that searches your files for patterns that you give it.
ripgrep is a tool I use in my daily workflow as a developer. In this post, I’d like to share my use cases of ripgrep.
Setup
I grabbed a ripgrep source archive from its GitHub repository and extracted it. I’ll be running the commands in this project.
$ curl -LO https://github.com/BurntSushi/ripgrep/archive/refs/tags/14.0.3.tar.gz
$ tar -xvf 14.0.3.tar.gz
$ cd ripgrep-14.0.3/
$ ls
benchsuite/ build.rs Cargo.lock Cargo.toml CHANGELOG.md ci/ COPYING crates/ Cross.toml doc/ FAQ.md GUIDE.md HomebrewFormula@ LICENSE-MIT pkg/ README.md RELEASE-CHECKLIST.md rustfmt.toml scripts/ tests/ UNLICENSE
Use Cases
Searching within a single file
To search for a specific pattern within a single file, the command is straightforward:
$ rg 'ripgrep\w+' README.md
36:[![A screenshot of a sample search with ripgrep](https://burntsushi.net/stuff/ripgrep1.png)](https://burntsushi.net/stuff/ripgrep1.png)
294:$ curl -LO https://github.com/BurntSushi/ripgrep/releases/download/13.0.0/ripgrep_13.0.0_amd64.deb
295:$ sudo dpkg -i ripgrep_13.0.0_amd64.deb
358:$ sudo pkgman install ripgrep_x86
This command finds all occurrences of “ripgrep” followed by any word character in the README.md file.
Utilizing glob patterns
ripgrep
allows for refined searches using glob patterns. For example, to find the pattern
“termcolor” in all TOML files:
$ rg termcolor -g '*.toml'
Cargo.toml
59:termcolor = "1.1.0"
crates/grep/Cargo.toml
25:termcolor = "1.0.4"
crates/printer/Cargo.toml
26:termcolor = "1.3.0"
crates/cli/Cargo.toml
20:termcolor = "1.3.0"
Glob patterns can limit the search to specific file types.
Filtering by file type
To find the pattern “fn run” in Rust files:
$ rg 'fn run' --type rust
crates/ignore/src/walk.rs
1205: pub fn run<'s, F>(self, mkf: F)
1498: fn run(mut self) {
1506: fn run_one(&mut self, mut work: Work) -> WalkState {
crates/core/main.rs
77:fn run(result: crate::flags::ParseResult<HiArgs>) -> anyhow::Result<ExitCode> {
crates/core/flags/doc/version.rs
83:fn runtime_cpu_features() -> Vec<String> {
crates/searcher/src/searcher/glue.rs
38: pub(crate) fn run(mut self) -> Result<(), S::Error> {
108: pub(crate) fn run(mut self) -> Result<(), S::Error> {
157: pub(crate) fn run(mut self) -> Result<(), S::Error> {
Alternatively, you can use the shorthand -trust
for the same result:
$ rg 'fn run' -trust
To exclude certain file types, you can use the --type-not
option:
$ rg 'fn run' --type-not rust
GUIDE.md
332:$ rg 'fn run' -g '*.rs'
339:$ rg 'fn run' --type rust
345:$ rg 'fn run' -trust
or, you can use the short form,
$ rg 'fn run' -Trust
For a comprehensive list of file types ripgrep
recognizes, run:
$ rg --type-list
agda: *.agda, *.lagda
aidl: *.aidl
amake: *.bp, *.mk
asciidoc: *.adoc, *.asc, *.asciidoc
asm: *.S, *.asm, *.s
asp: *.ascx, *.ascx.cs, *.ascx.vb, *.aspx, *.aspx.cs, *.aspx.vb
ats: *.ats, *.dats, *.hats, *.sats
avro: *.avdl, *.avpr, *.avsc
awk: *.awk
bazel: *.BUILD, *.bazel, *.bazelrc, *.bzl, BUILD, WORKSPACE
...
Note that you can add your own type to this list. For example, to add a custom mytype
type:
$ rg 'libc' --type-add=mytype:'*.{rs,toml}*' -tmytype
crates/cli/src/hostname.rs
15:/// `libc` linked into the program.
41: let limit = unsafe { libc::sysconf(libc::_SC_HOST_NAME_MAX) };
60: libc::gethostname(buf.as_mut_ptr().cast::<libc::c_char>(), maxlen)
crates/core/main.rs
20:// use the system allocator. On Linux, this would normally be glibc's
23:// difference (for ripgrep's purposes) between glibc's allocator and jemalloc.
crates/cli/Cargo.toml
25:[target.'cfg(unix)'.dependencies.libc]
Listing files with matches
To print paths that contains at least one match:
$ rg 'fn run' -trust --files-with-matches
crates/ignore/src/walk.rs
crates/searcher/src/searcher/glue.rs
crates/core/flags/doc/version.rs
crates/core/main.rs
Additional notes
ripgrep
searches recursively in the current directory by default.- When searching within a directory,
ripgrep
respects.gitignore
, and ignores hidden files and binaries.