I use RipGrep all the time, but sometimes when I want to do something I have to search the internet to find out.
Well, no more! I made this cheatsheet for myself. Maybe it'll help you.
Ripgrep search for specific file types
You want to find out where the AWS ARN
123456789012 is used. You have a mono-repo with many file types in it. You're only interested in Terraform files.
Solution globbing for file types
rg '123456789012' -g '*.tf'
This globs through all files that end with
.tf (the Terraform extension) for the ARN.
You want to search for the API endpoint "localhost:4531" through all Rust files.
Solution using Ripgrep's types
Ripgrep comes with a number of filetypes built in. You can do:
rg "localhost:4531" --type rust
# or more succinctly
rg "localhost:4531" --trust
You can find the full list of file types with
You want to find where the ARN is used, but want to ignore all markdown files.
Solution using inverse type selection
rg '123456789012' --type-not markdown
Ripgrep supports regex search by default.
Literal string (no regex)
Ripgrep by default uses regex to search. Sometimes the word we want to find contains valid regex, so this is an issue.
We can search literally with:
Show lines around the found text
Sometimes we want to search for something, and we'd like context on the found text in the file.
To find 1 line before our matched text:
To find 1 line after our matched text:
To find 1 line before and after our text:
Get statistics of a search
I use this to work out how much work it would be to go through my search.
So searching "crypto" would take a while. How about
crypto in Python files? This helps me speed up finding things.
rg "crypto" --stats
.... (full output of the search)
1083 matched lines
232 files contained matches
36826 files searched
6296587 bytes printed
254562478 bytes searched
5.805867 seconds spent searching
Exclude a directory
I do not want to search through our modules directory, only our code.
We can do this by:
$ rg crypto -g '!modules/' -g '!pypi/'
Find all files that have the word "cluster" in them.
rg --files | rg cluster