Scripts2026年4月11日·1 分钟阅读

ripgrep (rg) — Recursively Search Directories with Regex

ripgrep recursively searches directories for a regex pattern while respecting your gitignore. Written in Rust, ripgrep is the fastest search tool on the market — used inside VS Code, GitHub, and many other dev tools.

SC
Script Depot · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

# Install
brew install ripgrep                    # macOS
sudo apt install ripgrep                # Debian/Ubuntu
cargo install ripgrep                   # From source

Basic usage:

rg pattern                              # Search cwd recursively
rg -i pattern                           # Case-insensitive
rg "pattern" --type js                # Only .js files
rg -l pattern                           # List files with match
rg -C 3 pattern                         # 3 lines of context
rg pattern -g "!node_modules/*"       # Exclude dir
rg -r "\$1" "console.log\((.+)\)"  # Regex capture replace
rg --json pattern | jq                 # JSON output
介绍

ripgrep (binary name rg) is a line-oriented search tool that recursively searches directories for a regex pattern while respecting your .gitignore. Written in Rust by Andrew Gallant (BurntSushi), it combines the raw speed of grep with the usability of ag/ack. The search engine inside VS Code, GitHub code search, Helix editor, and many others.

What ripgrep Does

  • Recursive search — walks directory tree
  • Gitignore aware — skips .gitignore, .ignore, global ignore
  • Smart case--smart-case for case-insensitive when all lower
  • File types--type js or --type-add custom
  • Globs-g include/exclude patterns
  • Regex — full Rust regex engine (PCRE2 with -P)
  • Context-A/-B/-C lines around matches
  • Output — text, JSON, vimgrep, colored
  • Parallelism — multi-threaded by default

Architecture

Single Rust binary. Uses memchr + SIMD for byte-level search, regex crate for patterns. Walks filesystem in parallel (one thread per CPU). Respects .gitignore via the ignore crate.

Self-Hosting

CLI tool, self-contained binary.

Key Features

  • Fastest grep alternative (sometimes 10x faster than grep)
  • Gitignore/ignore respect
  • Parallel search
  • JSON output mode
  • File type filters
  • Unicode aware
  • PCRE2 support (with -P)
  • Replace mode (-r)
  • Integrates with VS Code, Helix, Zed, IntelliJ

Comparison

Tool Language Gitignore Speed Regex
ripgrep (rg) Rust Yes Fastest Rust regex + PCRE2
The Silver Searcher (ag) C Yes Fast PCRE
ack Perl No (manual) Slow Perl regex
grep C No OK POSIX/PCRE
git grep C Git-aware Fast Git regex

常见问题 FAQ

Q: 和 grep 比快多少? A: 一般 2-10 倍。大仓库(Linux kernel 级)差距更大,因为 ripgrep 并行且跳过 .gitignore。

Q: 能搜隐藏文件? A: 默认跳过。加 -.--hidden 启用。

Q: 为什么是公共领域许可? A: 作者 BurntSushi 的选择。Unlicense 等同于放弃版权,完全自由使用。

来源与致谢 Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产