Skip to content

ripgrep

Created: 2018-04-17 08:59:37 -0700 Modified: 2021-11-02 20:38:54 -0700

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.

  • -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”.

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

> "C:Usersagd13_000Downloadsripgrep-0.8.1-i686-pc-windows-msvcrg.exe" windowSizeInitialState
packagesclienttestcreatemockstore.js
24:import { windowSizeInitialState } from '../public/javascripts/react_and_redux/reducers/windowsize';
66: windowSizeReducer: windowSizeInitialState,
packagesclientpublicjavascriptsreact_and_reduxreducerswindowsize.js
3:export const windowSizeInitialState = {
8:export default function windowSizeReducer(state = windowSizeInitialState, action = null) {

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:

> "C:Usersagd13_000Downloadsripgrep-0.8.1-i686-pc-windows-msvcrg.exe" windowSizeInitialState --no-heading
packagesclienttestcreatemockstore.js:24:import { windowSizeInitialState } from '../public/javascripts/react_and_redux/reducers/windowsize';
packagesclienttestcreatemockstore.js:66: windowSizeReducer: windowSizeInitialState,
packagesclientpublicjavascriptsreact_and_reduxreducerswindowsize.js:3:export const windowSizeInitialState = {
packagesclientpublicjavascriptsreact_and_reduxreducerswindowsize.js:8:export default function windowSizeReducer(state = windowSizeInitialState, action = null) {

For information on what this does (from the official “—help”):

--no-heading
Don't group matches by each file. If --no-heading is provided in addition to
the -H/--with-filename flag, then file paths will be printed as a prefix for
every matched line. This is the default mode when not printing to a terminal.
This overrides the --heading flag.

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”.