ripgrep
Created: 2018-04-17 08:59:37 -0700 Modified: 2021-11-02 20:38:54 -0700
Background (reference)
Section titled Background (reference)ripgrep is basically grep, but it automatically ignores everything in “gitignore” by default, so you don’t need to have some pre-customized grep command aliased.
Previously, I used to do something like this to search through code:
grep -r -n —color —exclude={bundle*,log*,.map,.html,.min.js,.bak,TODO,TODO_archive} —exclude-dir={node_modules,.git,dist,logs,coverage,lib,vendor} %* ./
Then, one day I was looking for crucial code that happened to be in a folder called “logic”, but because that was excluded above thanks to “log*”, I got mad and switched to ripgrep.
Common options (reference)
Section titled Common options (reference)- -i: case-insensitive search
- -C<num>: show context of <num> lines before and after the result
- -tjs: specify that you only want to find “.js” files (this can be used with any extension; it’s not language-aware).
- -Tjs: specify that you don’t want to find any “.js” files
- —: separate any other arguments from the text to search for. This is really useful if you’re searching for something starting with a hyphen already, e.g. “rg — -help”.
Configuration file (reference)
Section titled Configuration file (reference)If you find yourself specifying the same flags all the time, you can make a configuration file by following the directions at the reference link.
A few notes
- Your environment variable has to point directly to the file. I named mine “config.ini” so that it would be highlighted mostly correctly in Sublime: C:toolsripgrepconfig.ini
- Options that I like
- —no-heading
Heading (or no heading) (—no-heading)
Section titled Heading (or no heading) (—no-heading)By default, ripgrep formats its output like this
On Windows, the filenames are clickable, but you can’t navigate to individual lines. To fix this, you can specify “—no-heading” and your output will look like this:
For information on what this does (from the official “—help”):
Including / excluding anything (reference)
Section titled Including / excluding anything (reference)If you still want to exclude something that’s not necessarily defined in .gitignore, then you can use the “-g” flag (and for exclusions, you need to start with an ”!“):
rg -g !log -g !logs somethingToSearchFor
This will ignore the “log” and “logs” folder.
If you want case-insensitive globs, use “—iglob”.