Intro to ripgrep
Ripgrep is a command-line tool for searching files that combines the usability of The Silver Searcher with the raw speed of GNU grep. Written in Rust, it is designed to be fast by default and offers a powerful way to search for patterns in your codebase.
Key Features
- Recursively searches directories by default, ignoring hidden and binary files.
- Respects
.gitignorerules automatically, so you don’t waste time searching files that aren’t part of your project. - Written in Rust, which contributes to its high performance.
- Outputs results in a clear, easy-to-read format.
Basic Usage
The basic syntax for ripgrep is simple:
rg [OPTIONS] PATTERN [PATH]
PATTERNis the text or regular expression you want to search for.PATHis the directory or file you want to search in. If you don’t specify a path, it will search the current directory.
Simple Search Example
To find the word hello in all files in the current directory:
rg hello
Common Options
**-ior--ignore-case**: Performs a case-insensitive search.**-wor--word-regexp**: Searches for a whole word, sorg -w 'the'won’t matchthere.**-nor--line-number**: Shows the line number for each match. This is often on by default.**--vimgrep**: Formats the output to be easily parsed byvimoremacs.**--type**: Restricts the search to a specific file type (e.g.,rg --type js 'console.log').
Advanced Filtering
Beyond the basics, ripgrep’s true power lies in its extensive filtering options that allow you to precisely control your searches.
Filtering by File Type
ripgrep has a built-in type system that lets you restrict your search to specific file types without having to list file extensions. You can see the full list by running rg --type-list.
- To search only within JavaScript files:
rg --type js 'my_function' - To search within multiple types, use the
--typeflag multiple times. For example, to search for a pattern in both Python and Rust files:rg --type py --type rs 'some_pattern'
Excluding Files and Directories
You can use glob patterns to exclude specific files or directories from your search.
- To ignore a specific directory:
rg 'my_pattern' --ignore-dir 'node_modules' - To ignore files that match a glob pattern, for example, all Markdown files:
The
rg 'TODO' --glob '!*.md'!negates the pattern, so!*.mdmeans “don’t search any file that ends with.md.”
ripgrep automatically respects .gitignore, .ignore, and .rgignore files.
Regular Expressions
ripgrep uses the fast Rust regex engine. Simply enclose the pattern in single quotes to prevent your shell from interpreting special characters.
- Search for a pattern that matches the word
funcfollowed by any word characters:rg 'func\w+' - To search for a pattern across multiple lines, use the
-Uor--multilineflag:rg -U 'fn main\(\) \{[\s\S]*?\}'