ripgrep
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRipgrep (rg) - Fast Text Search Tool
Ripgrep (rg) - 快速文本搜索工具
Overview
概述
Ripgrep is a line-oriented search tool that recursively searches directories for regex patterns. It's 10-100x faster than grep and respects by default. Use it instead of grep, find, or manually reading large files.
.gitignoreCore principle: When you need to find text in files, use ripgrep. Don't read entire files into context when you can search them.
Ripgrep 是一款面向行的搜索工具,可递归搜索目录中的 regex 模式。它的速度比 grep 快10-100倍,并且默认尊重 规则。可以用它替代 grep、find 或手动读取大文件。
.gitignore核心原则: 当你需要在文件中查找文本时,使用 ripgrep。如果可以通过搜索找到内容,就不要把整个文件读入上下文。
When to Use
使用场景
Use ripgrep when:
- Searching for text patterns across a codebase or directory
- Finding all occurrences of a function, variable, or string
- Searching through books, documentation, or large text files
- Files are too large to read fully into context
- Looking for specific content in many files at once
- Finding files that contain (or don't contain) certain patterns
- Extracting matching lines for analysis
Don't use when:
- You need the full file content (use Read tool)
- Simple glob pattern matching for filenames only (use Glob tool)
- You need structured data extraction (consider jq, awk)
以下情况使用 ripgrep:
- 在代码库或目录中搜索文本模式
- 查找函数、变量或字符串的所有出现位置
- 搜索书籍、文档或大型文本文件
- 文件过大,无法完整读入上下文
- 同时在多个文件中查找特定内容
- 查找包含(或不包含)特定模式的文件
- 提取匹配行用于分析
以下情况不使用:
- 需要完整文件内容(使用读取工具)
- 仅对文件名进行简单 glob 模式匹配(使用 Glob 工具)
- 需要结构化数据提取(考虑使用 jq、awk)
Quick Reference
快速参考
| Task | Command |
|---|---|
| Basic search | |
| Case insensitive | |
| Smart case (auto) | |
| Whole word only | |
| Fixed string (no regex) | |
| Show context lines | |
| Show line numbers | |
| Only filenames | |
| Files without match | |
| Count matches | |
| Only matching part | |
| Invert match | |
| Multiline search | |
| 任务 | 命令 |
|---|---|
| 基础搜索 | |
| 忽略大小写 | |
| 智能大小写(自动适配) | |
| 仅匹配整词 | |
| 固定字符串(不使用正则) | |
| 显示上下文行 | |
| 显示行号 | |
| 仅显示文件名 | |
| 显示无匹配的文件 | |
| 统计匹配次数 | |
| 仅显示匹配部分 | |
| 反向匹配 | |
| 多行搜索 | |
File Filtering
文件过滤
By File Type
按文件类型
Ripgrep has built-in file type definitions. Use to include, to exclude:
-t-Tbash
undefinedRipgrep 内置了文件类型定义。使用 包含指定类型, 排除指定类型:
-t-Tbash
undefinedSearch only Python files
仅搜索Python文件
rg -t py "def main"
rg -t py "def main"
Search only JavaScript and TypeScript
仅搜索JavaScript和TypeScript文件
rg -t js -t ts "import"
rg -t js -t ts "import"
Exclude test files
排除测试文件
rg -T test "function"
rg -T test "function"
List all known types
列出所有支持的文件类型
rg --type-list
**Common types:** `py`, `js`, `ts`, `rust`, `go`, `java`, `c`, `cpp`, `rb`, `php`, `html`, `css`, `json`, `yaml`, `md`, `txt`, `sh`rg --type-list
**常见类型:** `py`, `js`, `ts`, `rust`, `go`, `java`, `c`, `cpp`, `rb`, `php`, `html`, `css`, `json`, `yaml`, `md`, `txt`, `sh`By Glob Pattern
按Glob模式
bash
undefinedbash
undefinedOnly .tsx files
仅搜索.tsx文件
rg -g "*.tsx" "useState"
rg -g "*.tsx" "useState"
Exclude node_modules (in addition to gitignore)
排除node_modules目录(在gitignore规则基础上额外排除)
rg -g "!node_modules/**" "pattern"
rg -g "!node_modules/**" "pattern"
Only files in src directory
仅搜索src目录下的文件
rg -g "src/**" "pattern"
rg -g "src/**" "pattern"
Multiple globs
多个Glob规则
rg -g ".js" -g ".ts" "pattern"
rg -g ".js" -g ".ts" "pattern"
Case insensitive globs
忽略大小写的Glob规则
rg --iglob "*.JSON" "pattern"
undefinedrg --iglob "*.JSON" "pattern"
undefinedBy File Size
按文件大小
bash
undefinedbash
undefinedSkip files larger than 1MB
跳过大于1MB的文件
rg --max-filesize 1M "pattern"
undefinedrg --max-filesize 1M "pattern"
undefinedDirectory Control
目录控制
bash
undefinedbash
undefinedLimit depth
限制搜索深度
rg --max-depth 2 "pattern"
rg --max-depth 2 "pattern"
Search hidden files (dotfiles)
搜索隐藏文件(点文件)
rg --hidden "pattern"
rg --hidden "pattern"
Follow symlinks
跟随符号链接
rg -L "pattern"
rg -L "pattern"
Ignore all ignore files (.gitignore, etc.)
忽略所有忽略文件(.gitignore等)
rg --no-ignore "pattern"
rg --no-ignore "pattern"
Progressive unrestricted (-u can stack up to 3 times)
逐步解除限制(-u最多可叠加3次)
rg -u "pattern" # --no-ignore
rg -uu "pattern" # --no-ignore --hidden
rg -uuu "pattern" # --no-ignore --hidden --binary
undefinedrg -u "pattern" # --no-ignore
rg -uu "pattern" # --no-ignore --hidden
rg -uuu "pattern" # --no-ignore --hidden --binary
undefinedContext Options
上下文选项
bash
undefinedbash
undefinedLines after match
显示匹配行之后的N行
rg -A 5 "pattern"
rg -A 5 "pattern"
Lines before match
显示匹配行之前的N行
rg -B 5 "pattern"
rg -B 5 "pattern"
Lines before and after
显示匹配行前后的N行
rg -C 5 "pattern"
rg -C 5 "pattern"
Print entire file on match (passthrough mode)
匹配时打印整个文件内容(直通模式)
rg --passthru "pattern"
undefinedrg --passthru "pattern"
undefinedOutput Formats
输出格式
bash
undefinedbash
undefinedJust filenames with matches
仅显示包含匹配的文件名
rg -l "pattern"
rg -l "pattern"
Files without matches
显示无匹配的文件名
rg --files-without-match "pattern"
rg --files-without-match "pattern"
Count matches per file
按文件统计匹配次数
rg -c "pattern"
rg -c "pattern"
Count total matches (not lines)
统计总匹配次数(不是行数)
rg --count-matches "pattern"
rg --count-matches "pattern"
Only the matched text (not full line)
仅显示匹配的文本(不是整行)
rg -o "pattern"
rg -o "pattern"
JSON output (for parsing)
JSON格式输出(用于解析)
rg --json "pattern"
rg --json "pattern"
Vim-compatible output (file:line:col:match)
兼容Vim的输出格式(file:line:col:match)
rg --vimgrep "pattern"
rg --vimgrep "pattern"
With statistics
显示统计信息
rg --stats "pattern"
undefinedrg --stats "pattern"
undefinedRegex Patterns
正则表达式模式
Ripgrep uses Rust regex syntax by default:
bash
undefinedRipgrep 默认使用 Rust 正则表达式语法:
bash
undefinedAlternation
分支匹配
rg "foo|bar"
rg "foo|bar"
Character classes
字符类
rg "[0-9]+"
rg "[a-zA-Z_][a-zA-Z0-9_]*"
rg "[0-9]+"
rg "[a-zA-Z_][a-zA-Z0-9_]*"
Word boundaries
单词边界
rg "\bword\b"
rg "\bword\b"
Quantifiers
量词
rg "colou?r" # 0 or 1
rg "go+gle" # 1 or more
rg "ha*" # 0 or more
rg "x{2,4}" # 2 to 4 times
rg "colou?r" # 0或1次
rg "go+gle" # 1或多次
rg "ha*" # 0或多次
rg "x{2,4}" # 2到4次
Groups
分组
rg "(foo|bar)baz"
rg "(foo|bar)baz"
Lookahead/lookbehind (requires -P for PCRE2)
正向预查/反向预查(需要-P参数启用PCRE2)
rg -P "(?<=prefix)content"
rg -P "content(?=suffix)"
undefinedrg -P "(?<=prefix)content"
rg -P "content(?=suffix)"
undefinedMultiline Matching
多行匹配
bash
undefinedbash
undefinedEnable multiline mode
启用多行模式
rg -U "start.*\nend"
rg -U "start.*\nend"
Dot matches newline too
让点号匹配换行符
rg -U --multiline-dotall "start.*end"
rg -U --multiline-dotall "start.*end"
Match across lines
跨多行匹配
rg -U "function\s+\w+([^)])\s{"
undefinedrg -U "function\s+\w+([^)])\s{"
undefinedReplacement (Preview Only)
替换(仅预览)
Ripgrep can show what replacements would look like (doesn't modify files):
bash
undefinedRipgrep 可以显示替换后的效果(不会修改文件):
bash
undefinedSimple replacement
简单替换
rg "old" -r "new"
rg "old" -r "new"
Using capture groups
使用捕获组
rg "(\w+)@(\w+)" -r "$2::$1"
rg "(\w+)@(\w+)" -r "$2::$1"
Remove matches (empty replacement)
移除匹配内容(空替换)
rg "pattern" -r ""
undefinedrg "pattern" -r ""
undefinedSearching Special Files
搜索特殊文件
Compressed Files
压缩文件
bash
undefinedbash
undefinedSearch in gzip, bzip2, xz, lz4, lzma, zstd files
在gzip、bzip2、xz、lz4、lzma、zstd格式的文件中搜索
rg -z "pattern" file.gz
rg -z "pattern" archive.tar.gz
undefinedrg -z "pattern" file.gz
rg -z "pattern" archive.tar.gz
undefinedBinary Files
二进制文件
bash
undefinedbash
undefinedInclude binary files
包含二进制文件
rg --binary "pattern"
rg --binary "pattern"
Treat binary as text (may produce garbage)
将二进制文件视为文本(可能产生乱码)
rg -a "pattern"
undefinedrg -a "pattern"
undefinedLarge Files
大文件
For files too large to read into context:
bash
undefined对于过大无法读入上下文的文件:
bash
undefinedSearch and show only matching lines
搜索并仅显示匹配行
rg "specific pattern" large_file.txt
rg "specific pattern" large_file.txt
Limit matches to first N per file
限制每个文件最多显示N个匹配结果
rg -m 10 "pattern" huge_file.log
rg -m 10 "pattern" huge_file.log
Show byte offset for large file navigation
显示匹配内容的字节偏移量,方便大文件导航
rg -b "pattern" large_file.txt
rg -b "pattern" large_file.txt
Use with head/tail for pagination
结合head/tail进行分页
rg "pattern" large_file.txt | head -100
undefinedrg "pattern" large_file.txt | head -100
undefinedPerformance Tips
性能优化技巧
- Be specific with paths - Don't search from root when you know the subdir
- Use file types - is faster than
-t py-g "*.py" - Use fixed strings - when you don't need regex
-F - Limit depth - when you know structure
--max-depth - Let gitignore work - Don't use unless needed
--no-ignore - Use word boundaries - is optimized
-w
- 指定具体路径 - 当你知道目标子目录时,不要从根目录开始搜索
- 使用文件类型参数 - 比
-t py速度更快-g "*.py" - 使用固定字符串模式 - 不需要正则时使用
-F - 限制搜索深度 - 了解目录结构时使用
--max-depth - 保留gitignore规则 - 除非必要,否则不要使用
--no-ignore - 使用单词边界 - 参数经过优化,速度更快
-w
Common Patterns
常见使用场景示例
Find function definitions
查找函数定义
bash
undefinedbash
undefinedPython
Python
rg "def \w+(" -t py
rg "def \w+(" -t py
JavaScript/TypeScript
JavaScript/TypeScript
rg "(function|const|let|var)\s+\w+\s*=" -t js -t ts
rg "^\s*(async\s+)?function" -t js
rg "(function|const|let|var)\s+\w+\s*=" -t js -t ts
rg "^\s*(async\s+)?function" -t js
Go
Go
rg "^func\s+\w+" -t go
undefinedrg "^func\s+\w+" -t go
undefinedFind imports/requires
查找导入语句
bash
undefinedbash
undefinedPython
Python
rg "^(import|from)\s+" -t py
rg "^(import|from)\s+" -t py
JavaScript
JavaScript
rg "^(import|require()" -t js
rg "^(import|require()" -t js
Go
Go
rg "^import\s+" -t go
undefinedrg "^import\s+" -t go
undefinedFind TODO/FIXME comments
查找TODO/FIXME注释
bash
rg "(TODO|FIXME|HACK|XXX):"bash
rg "(TODO|FIXME|HACK|XXX):"Find error handling
查找错误处理代码
bash
undefinedbash
undefinedPython
Python
rg "except\s+\w+:" -t py
rg "except\s+\w+:" -t py
JavaScript
JavaScript
rg ".catch(|catch\s*(" -t js
undefinedrg ".catch(|catch\s*(" -t js
undefinedFind class definitions
查找类定义
bash
undefinedbash
undefinedPython
Python
rg "^class\s+\w+" -t py
rg "^class\s+\w+" -t py
JavaScript/TypeScript
JavaScript/TypeScript
rg "^(export\s+)?(default\s+)?class\s+\w+" -t js -t ts
undefinedrg "^(export\s+)?(default\s+)?class\s+\w+" -t js -t ts
undefinedSearch in books/documents
在书籍/文档中搜索
bash
undefinedbash
undefinedFind chapter headings
查找章节标题
rg "^(Chapter|CHAPTER)\s+\d+" book.txt
rg "^(Chapter|CHAPTER)\s+\d+" book.txt
Find quoted text
查找引用文本
rg '"[^"]{20,}"' document.txt
rg '"[^"]{20,}"' document.txt
Find paragraphs containing word
查找包含指定关键词的段落
rg -C 2 "keyword" book.txt
undefinedrg -C 2 "keyword" book.txt
undefinedCombining with Other Tools
与其他工具结合使用
bash
undefinedbash
undefinedFind files, then search
先查找文件,再在文件中搜索
rg --files | xargs rg "pattern"
rg --files | xargs rg "pattern"
Search and count by file
搜索并按文件统计排序
rg -c "pattern" | sort -t: -k2 -rn
rg -c "pattern" | sort -t: -k2 -rn
Search and open in editor
搜索并在编辑器中打开文件
rg -l "pattern" | xargs code
rg -l "pattern" | xargs code
Extract unique matches
提取唯一匹配结果
rg -o "\b[A-Z]{2,}\b" | sort -u
rg -o "\b[A-Z]{2,}\b" | sort -u
Search multiple patterns from file
从文件中读取多个模式进行搜索
rg -f patterns.txt
undefinedrg -f patterns.txt
undefinedExit Codes
退出码
| Code | Meaning |
|---|---|
| 0 | Matches found |
| 1 | No matches found |
| 2 | Error occurred |
Useful for scripting:
bash
if rg -q "pattern" file.txt; then
echo "Found"
fi| 代码 | 含义 |
|---|---|
| 0 | 找到匹配结果 |
| 1 | 未找到匹配结果 |
| 2 | 发生错误 |
适用于脚本编写:
bash
if rg -q "pattern" file.txt; then
echo "找到匹配"
fiCommon Mistakes
常见错误及解决方法
| Mistake | Fix |
|---|---|
| Pattern has special chars | Use |
| Can't find hidden files | Add |
| Missing node_modules | Add |
| Regex too complex | Try |
| Output too long | Use |
| Binary file skipped | Add |
| Need to see full line | Remove |
| 错误 | 解决方法 |
|---|---|
| 模式包含特殊字符 | 使用 |
| 无法找到隐藏文件 | 添加 |
| 无法搜索node_modules目录 | 添加 |
| 正则表达式过于复杂 | 尝试使用 |
| 输出内容过多 | 使用 |
| 二进制文件被跳过 | 添加 |
| 需要查看完整行 | 移除 |
When to Prefer Other Tools
何时优先使用其他工具
| Task | Better Tool |
|---|---|
| Structured JSON queries | |
| Column-based text processing | |
| Stream editing/substitution | |
| Find files by name only | |
| Simple file listing | |
| Full file content needed | Read tool |
| 任务 | 更合适的工具 |
|---|---|
| 结构化JSON查询 | |
| 基于列的文本处理 | |
| 流编辑/替换(实际修改文件) | |
| 仅按文件名查找文件 | |
| 简单文件列表 | |
| 需要完整文件内容 | 读取工具 |