ripgrep by ratacat/claude-skills
npx skills add https://github.com/ratacat/claude-skills --skill ripgrepRipgrep 是一个面向行的搜索工具,可递归搜索目录中的正则表达式模式。它比 grep 快 10-100 倍,并且默认遵守 .gitignore 规则。用它来替代 grep、find 或手动读取大文件。
核心原则: 当您需要在文件中查找文本时,请使用 ripgrep。在可以搜索的情况下,不要将整个文件读入上下文。
在以下情况下使用 ripgrep:
在以下情况下不要使用:
| 任务 | 命令 |
|---|---|
| 基本搜索 | rg "pattern" [path] |
| 不区分大小写 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
rg -i "pattern" |
| 智能大小写(自动) | rg -S "pattern" |
| 仅全词匹配 | rg -w "word" |
| 固定字符串(非正则表达式) | rg -F "literal.string" |
| 显示上下文行 | rg -C 3 "pattern"(前后各 3 行) |
| 显示行号 | rg -n "pattern"(在 tty 中默认启用) |
| 仅文件名 | rg -l "pattern" |
| 无匹配的文件 | rg --files-without-match "pattern" |
| 统计匹配次数 | rg -c "pattern" |
| 仅匹配部分 | rg -o "pattern" |
| 反向匹配 | rg -v "pattern" |
| 多行搜索 | rg -U "pattern.*\nmore" |
Ripgrep 内置了文件类型定义。使用 -t 包含,-T 排除:
# 仅搜索 Python 文件
rg -t py "def main"
# 仅搜索 JavaScript 和 TypeScript 文件
rg -t js -t ts "import"
# 排除测试文件
rg -T test "function"
# 列出所有已知类型
rg --type-list
常见类型: py, js, ts, rust, go, java, c, cpp, rb, php, html, css, json, yaml, md, txt, sh
# 仅 .tsx 文件
rg -g "*.tsx" "useState"
# 排除 node_modules(除了 gitignore 规则外)
rg -g "!node_modules/**" "pattern"
# 仅 src 目录中的文件
rg -g "src/**" "pattern"
# 多个 glob 模式
rg -g "*.js" -g "*.ts" "pattern"
# 不区分大小写的 glob 模式
rg --iglob "*.JSON" "pattern"
# 跳过大于 1MB 的文件
rg --max-filesize 1M "pattern"
# 限制深度
rg --max-depth 2 "pattern"
# 搜索隐藏文件(点文件)
rg --hidden "pattern"
# 跟随符号链接
rg -L "pattern"
# 忽略所有忽略文件(.gitignore 等)
rg --no-ignore "pattern"
# 渐进式解除限制(-u 最多可叠加 3 次)
rg -u "pattern" # --no-ignore
rg -uu "pattern" # --no-ignore --hidden
rg -uuu "pattern" # --no-ignore --hidden --binary
# 匹配行之后的行
rg -A 5 "pattern"
# 匹配行之前的行
rg -B 5 "pattern"
# 匹配行前后的行
rg -C 5 "pattern"
# 匹配时打印整个文件(直通模式)
rg --passthru "pattern"
# 仅显示包含匹配的文件名
rg -l "pattern"
# 不包含匹配的文件
rg --files-without-match "pattern"
# 统计每个文件的匹配次数
rg -c "pattern"
# 统计总匹配次数(非行数)
rg --count-matches "pattern"
# 仅匹配的文本(非整行)
rg -o "pattern"
# JSON 输出(用于解析)
rg --json "pattern"
# Vim 兼容输出(文件:行:列:匹配)
rg --vimgrep "pattern"
# 带统计信息
rg --stats "pattern"
Ripgrep 默认使用 Rust 正则表达式语法:
# 选择
rg "foo|bar"
# 字符类
rg "[0-9]+"
rg "[a-zA-Z_][a-zA-Z0-9_]*"
# 单词边界
rg "\bword\b"
# 量词
rg "colou?r" # 0 或 1 次
rg "go+gle" # 1 次或多次
rg "ha*" # 0 次或多次
rg "x{2,4}" # 2 到 4 次
# 分组
rg "(foo|bar)baz"
# 前瞻/后顾(需要 -P 启用 PCRE2)
rg -P "(?<=prefix)content"
rg -P "content(?=suffix)"
# 启用多行模式
rg -U "start.*\nend"
# 点号也匹配换行符
rg -U --multiline-dotall "start.*end"
# 跨行匹配
rg -U "function\s+\w+\([^)]*\)\s*\{"
Ripgrep 可以显示替换后的效果(不修改文件):
# 简单替换
rg "old" -r "new"
# 使用捕获组
rg "(\w+)@(\w+)" -r "$2::$1"
# 移除匹配项(空替换)
rg "pattern" -r ""
# 在 gzip、bzip2、xz、lz4、lzma、zstd 文件中搜索
rg -z "pattern" file.gz
rg -z "pattern" archive.tar.gz
# 包含二进制文件
rg --binary "pattern"
# 将二进制文件视为文本(可能产生乱码)
rg -a "pattern"
对于太大而无法读入上下文的文件:
# 搜索并仅显示匹配行
rg "specific pattern" large_file.txt
# 限制每个文件的匹配次数为前 N 个
rg -m 10 "pattern" huge_file.log
# 显示字节偏移以便在大文件中导航
rg -b "pattern" large_file.txt
# 与 head/tail 结合使用进行分页
rg "pattern" large_file.txt | head -100
-t py 比 -g "*.py" 更快-F--max-depth--no-ignore-w 经过了优化# Python
rg "def \w+\(" -t py
# JavaScript/TypeScript
rg "(function|const|let|var)\s+\w+\s*=" -t js -t ts
rg "^\s*(async\s+)?function" -t js
# Go
rg "^func\s+\w+" -t go
# Python
rg "^(import|from)\s+" -t py
# JavaScript
rg "^(import|require\()" -t js
# Go
rg "^import\s+" -t go
rg "(TODO|FIXME|HACK|XXX):"
# Python
rg "except\s+\w+:" -t py
# JavaScript
rg "\.catch\(|catch\s*\(" -t js
# Python
rg "^class\s+\w+" -t py
# JavaScript/TypeScript
rg "^(export\s+)?(default\s+)?class\s+\w+" -t js -t ts
# 查找章节标题
rg "^(Chapter|CHAPTER)\s+\d+" book.txt
# 查找引用的文本
rg '"[^"]{20,}"' document.txt
# 查找包含单词的段落
rg -C 2 "keyword" book.txt
# 查找文件,然后搜索
rg --files | xargs rg "pattern"
# 搜索并按文件计数
rg -c "pattern" | sort -t: -k2 -rn
# 搜索并在编辑器中打开
rg -l "pattern" | xargs code
# 提取唯一匹配项
rg -o "\b[A-Z]{2,}\b" | sort -u
# 从文件中搜索多个模式
rg -f patterns.txt
| 代码 | 含义 |
|---|---|
| 0 | 找到匹配项 |
| 1 | 未找到匹配项 |
| 2 | 发生错误 |
适用于脚本编写:
if rg -q "pattern" file.txt; then
echo "Found"
fi
| 错误 | 修复方法 |
|---|---|
| 模式包含特殊字符 | 对固定字符串使用 -F 或转义:rg "foo\.bar" |
| 找不到隐藏文件 | 添加 --hidden 或 -uu |
| 缺少 node_modules | 添加 --no-ignore(但通常跳过它是正确的) |
| 正则表达式太复杂 | 尝试使用 -P 启用 PCRE2 以支持前瞻/后顾 |
| 输出太长 | 使用 -m N 限制,或使用 -l 仅显示文件名 |
| 二进制文件被跳过 | 添加 --binary 或 -a 进入文本模式 |
| 需要查看整行 | 移除 -o(仅匹配)标志 |
| 任务 | 更好的工具 |
|---|---|
| 结构化 JSON 查询 | jq |
| 基于列的文本处理 | awk |
| 流编辑/替换 | sed(实际修改文件) |
| 仅按名称查找文件 | fd 或 find |
| 简单文件列表 | ls 或 glob |
| 需要完整文件内容 | Read 工具 |
每周安装次数
104
代码仓库
GitHub 星标数
25
首次出现
Jan 21, 2026
安全审计
安装于
opencode93
gemini-cli88
codex85
github-copilot83
cursor73
amp70
Ripgrep is a line-oriented search tool that recursively searches directories for regex patterns. It's 10-100x faster than grep and respects .gitignore by default. Use it instead of grep, find, or manually reading large files.
Core principle: When you need to find text in files, use ripgrep. Don't read entire files into context when you can search them.
Use ripgrep when:
Don't use when:
| Task | Command |
|---|---|
| Basic search | rg "pattern" [path] |
| Case insensitive | rg -i "pattern" |
| Smart case (auto) | rg -S "pattern" |
| Whole word only | rg -w "word" |
| Fixed string (no regex) | rg -F "literal.string" |
| Show context lines | rg -C 3 "pattern" (3 before & after) |
| Show line numbers | rg -n "pattern" (default in tty) |
| Only filenames | rg -l "pattern" |
| Files without match | rg --files-without-match "pattern" |
| Count matches | rg -c "pattern" |
| Only matching part | rg -o "pattern" |
| Invert match | rg -v "pattern" |
| Multiline search | rg -U "pattern.*\nmore" |
Ripgrep has built-in file type definitions. Use -t to include, -T to exclude:
# Search only Python files
rg -t py "def main"
# Search only JavaScript and TypeScript
rg -t js -t ts "import"
# Exclude test files
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, , , , ,
# Only .tsx files
rg -g "*.tsx" "useState"
# Exclude node_modules (in addition to gitignore)
rg -g "!node_modules/**" "pattern"
# Only files in src directory
rg -g "src/**" "pattern"
# Multiple globs
rg -g "*.js" -g "*.ts" "pattern"
# Case insensitive globs
rg --iglob "*.JSON" "pattern"
# Skip files larger than 1MB
rg --max-filesize 1M "pattern"
# Limit depth
rg --max-depth 2 "pattern"
# Search hidden files (dotfiles)
rg --hidden "pattern"
# Follow symlinks
rg -L "pattern"
# Ignore all ignore files (.gitignore, etc.)
rg --no-ignore "pattern"
# Progressive unrestricted (-u can stack up to 3 times)
rg -u "pattern" # --no-ignore
rg -uu "pattern" # --no-ignore --hidden
rg -uuu "pattern" # --no-ignore --hidden --binary
# Lines after match
rg -A 5 "pattern"
# Lines before match
rg -B 5 "pattern"
# Lines before and after
rg -C 5 "pattern"
# Print entire file on match (passthrough mode)
rg --passthru "pattern"
# Just filenames with matches
rg -l "pattern"
# Files without matches
rg --files-without-match "pattern"
# Count matches per file
rg -c "pattern"
# Count total matches (not lines)
rg --count-matches "pattern"
# Only the matched text (not full line)
rg -o "pattern"
# JSON output (for parsing)
rg --json "pattern"
# Vim-compatible output (file:line:col:match)
rg --vimgrep "pattern"
# With statistics
rg --stats "pattern"
Ripgrep uses Rust regex syntax by default:
# Alternation
rg "foo|bar"
# Character classes
rg "[0-9]+"
rg "[a-zA-Z_][a-zA-Z0-9_]*"
# Word boundaries
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
# Groups
rg "(foo|bar)baz"
# Lookahead/lookbehind (requires -P for PCRE2)
rg -P "(?<=prefix)content"
rg -P "content(?=suffix)"
# Enable multiline mode
rg -U "start.*\nend"
# Dot matches newline too
rg -U --multiline-dotall "start.*end"
# Match across lines
rg -U "function\s+\w+\([^)]*\)\s*\{"
Ripgrep can show what replacements would look like (doesn't modify files):
# Simple replacement
rg "old" -r "new"
# Using capture groups
rg "(\w+)@(\w+)" -r "$2::$1"
# Remove matches (empty replacement)
rg "pattern" -r ""
# Search in gzip, bzip2, xz, lz4, lzma, zstd files
rg -z "pattern" file.gz
rg -z "pattern" archive.tar.gz
# Include binary files
rg --binary "pattern"
# Treat binary as text (may produce garbage)
rg -a "pattern"
For files too large to read into context:
# Search and show only matching lines
rg "specific pattern" large_file.txt
# Limit matches to first N per file
rg -m 10 "pattern" huge_file.log
# Show byte offset for large file navigation
rg -b "pattern" large_file.txt
# Use with head/tail for pagination
rg "pattern" large_file.txt | head -100
-t py is faster than -g "*.py"-F when you don't need regex--max-depth when you know structure--no-ignore unless needed-w is optimized# Python
rg "def \w+\(" -t py
# JavaScript/TypeScript
rg "(function|const|let|var)\s+\w+\s*=" -t js -t ts
rg "^\s*(async\s+)?function" -t js
# Go
rg "^func\s+\w+" -t go
# Python
rg "^(import|from)\s+" -t py
# JavaScript
rg "^(import|require\()" -t js
# Go
rg "^import\s+" -t go
rg "(TODO|FIXME|HACK|XXX):"
# Python
rg "except\s+\w+:" -t py
# JavaScript
rg "\.catch\(|catch\s*\(" -t js
# Python
rg "^class\s+\w+" -t py
# JavaScript/TypeScript
rg "^(export\s+)?(default\s+)?class\s+\w+" -t js -t ts
# Find chapter headings
rg "^(Chapter|CHAPTER)\s+\d+" book.txt
# Find quoted text
rg '"[^"]{20,}"' document.txt
# Find paragraphs containing word
rg -C 2 "keyword" book.txt
# Find files, then search
rg --files | xargs rg "pattern"
# Search and count by file
rg -c "pattern" | sort -t: -k2 -rn
# Search and open in editor
rg -l "pattern" | xargs code
# Extract unique matches
rg -o "\b[A-Z]{2,}\b" | sort -u
# Search multiple patterns from file
rg -f patterns.txt
| Code | Meaning |
|---|---|
| 0 | Matches found |
| 1 | No matches found |
| 2 | Error occurred |
Useful for scripting:
if rg -q "pattern" file.txt; then
echo "Found"
fi
| Mistake | Fix |
|---|---|
| Pattern has special chars | Use -F for fixed strings or escape: rg "foo\.bar" |
| Can't find hidden files | Add --hidden or -uu |
| Missing node_modules | Add --no-ignore (but it's usually right to skip) |
| Regex too complex | Try -P for PCRE2 with lookahead/lookbehind |
| Output too long | Use -m N to limit, or for just filenames |
| Task | Better Tool |
|---|---|
| Structured JSON queries | jq |
| Column-based text processing | awk |
| Stream editing/substitution | sed (actually modifies files) |
| Find files by name only | fd or find |
| Simple file listing | ls or glob |
Weekly Installs
104
Repository
GitHub Stars
25
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubWarnSocketPassSnykPass
Installed on
opencode93
gemini-cli88
codex85
github-copilot83
cursor73
amp70
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
116,600 周安装
Python uv包管理工具使用指南:替代pip和poetry的现代依赖管理方案
224 周安装
Android ViewModel 状态管理指南:使用 StateFlow 与 SharedFlow 实现 UI 状态与事件处理
224 周安装
Shape Up产品管理方法:告别估算陷阱,实现高效交付的产品开发框架
103 周安装
TLDR Stats - Claude AI 令牌使用监控与成本节省分析仪表盘工具
225 周安装
Agentica SDK 中文指南:Python AI 智能体开发框架,支持多智能体编排与工具集成
225 周安装
Continuous Claude系统概览:AI助手工作原理、记忆层与钩子机制详解
228 周安装
jsonyamlmdtxtsh-l| Binary file skipped | Add --binary or -a for text mode |
| Need to see full line | Remove -o (only-matching) flag |
| Full file content needed | Read tool |