Contents
ripgrep (rg) is a search tool that recursively searches directories while applying exclusion rules such as .gitignore. Start with rg 'search term', and if some files are missing from the results, check the search filtering settings.
Key Points#
- By default,
rgreads.gitignore,.ignore, and.rgignore, and excludes hidden files and binary files from searches. - Use
-tto specify a file type and-nto display line numbers. - Use
-uuuto ignore exclusion rules and search everything. - The default regular expression engine does not support lookahead or backreferences; enable PCRE2 if you need them.
Run Your First Search#
From the project root, pass the string you want to find. Without any special options, ripgrep searches files recursively and displays file names and matching lines.
rg 'TODO'
rg -n 'TODO'The second command also prints line numbers. To search specific files, specify their paths after the pattern. Regular expressions are also supported; for example, you can use [A-Z]+_SUSPEND to find uppercase identifiers.
Narrow the Search#
Limiting searches by extension or file type makes results easier to read. Use -tpy to search only Python files, or -Tjs to exclude JavaScript.
rg -tpy 'def main'
rg -Tjs 'fetch'You can configure supported types and custom type rules. In large repositories, narrowing the scope first helps avoid unwanted results.
Find Excluded Files#
By default, rg skips hidden files and binary files, as well as files matching rules such as .gitignore. If you expect a string in a configuration file or generated output but cannot find it, check whether exclusion settings are responsible.
Add -uuu to disable all automatic filtering. This can greatly increase the amount of data searched, so it is practical to use it only when needed.
rg -uuu '設定値'Regular Expressions and Special Input#
The default regular expression engine supports Unicode and performs common searches quickly. However, it does not support lookahead or backreferences. If you need these features, use -P with a version of rg built with PCRE2 support.
rg -P '(?<=user_id=)\d+'You do not need to use PCRE2 all the time. If you are unsure whether it was enabled at build time, check rg --help or the information for your installed package.
You can also search text in encodings other than UTF-8 and compressed files. Use -E to specify the encoding and -z to search supported compressed files.
rg -E Shift_JIS '検索語' data.txt
rg -z '検索語' archive.gzSpeed and Choosing the Right Tool#
ripgrep is designed for fast searches using features such as Rust's regular expression engine and parallel directory traversal. In a Linux kernel comparison in the GitHub README, rg took 0.082 seconds and git grep took 0.273 seconds under the specified conditions. However, these figures come from a particular machine and search setup. You may not see the same difference in your own environment.
It runs on Windows, macOS, and Linux, but it is not a POSIX-standard tool and does not come preinstalled in every environment. Where installing additional software is difficult, standard grep may be easier to use.
Next Step#
Run rg -n 'TODO' from the project root. If you need to search files missing from the results, follow up with rg -uuu -n 'TODO'.
Source: https://github.com/burntsushi/ripgrep
Contents
