
ast-grep
CLI基于语法树(AST)的代码结构化搜索、lint 与批量改写工具:按代码模式而非文本匹配,用 $VAR 通配节点,适合 agent 做跨文件的大规模重构。
ast-grep(sg)
ast-grep(sg) is a CLI tool for code structural search, lint, and rewriting.
Introduction
ast-grep is an abstract syntax tree based tool to search code by pattern code. Think of it as your old-friend grep, but matching AST nodes instead of text.
You can write patterns as if you are writing ordinary code. It will match all code that has the same syntactical structure.
You can use $ sign + upper case letters as a wildcard, e.g. $MATCH, to match any single AST node. Think of it as regular expression dot ., except it is not textual.
Try the online playground for a taste!
Screenshot

See more screenshots on the website.
Installation
You can install it from npm, pip, cargo, cargo-binstall, homebrew, scoop, mise or MacPorts!
npm install --global @ast-grep/cli
# `pnpm approve-builds` may be needed
pip install ast-grep-cli
brew install ast-grep
Click for more installation methods
cargo install ast-grep --locked
cargo binstall ast-grep
# install via scoop, thank @brian6932
scoop install main/ast-grep
# install via MacPorts
sudo port install ast-grep
# try ast-grep in nix-shell
nix-shell -p ast-grep
# try ast-grep with mise
mise use -g ast-grep
Or you can build ast-grep from source. You need to install rustup, clone the repository and then
cargo install --path ./crates/cli --locked
Packages are available on other platforms too.
Command line usage example
ast-grep has following form.
ast-grep --pattern 'var code = $PATTERN' --rewrite 'let code = new $PATTERN' --lang ts
Example
ast-grep -p '$A && $A()' -l ts -r '$A?.()'
ast-grep -p 'new Zodios($URL, $CONF as const,)' -l ts -r 'new Zodios($URL, $CONF)' -i
Sponsor
If you find ast-grep interesting and useful for your work, please buy me a coffee so I can spend more time on the project!
Feature Highlight
ast-grep's core is an algorithm to search and replace code based on abstract syntax tree produced by tree-sitter. It can help you to do lightweight static analysis and massive scale code manipulation in an intuitive way.
Key highlights:
-
An intuitive pattern to find and replace AST. ast-grep's pattern looks like ordinary code you would write every day (you could say the pattern is isomorphic to code).
-
jQuery like API for AST traversal and manipulation.
-
YAML configuration to write new linting rules or code modification.
-
Written in compiled language, with tree-sitter based parsing and utilizing multiple cores.
-
Beautiful command line interface :)
ast-grep's vision is to democratize abstract syntax tree magic and to liberate one from cumbersome AST programming!
- If you are an open-source library author, ast-grep can help your library users adopt breaking changes more easily.
- if you are a tech lead in your team, ast-grep can help you enforce code best practice tailored to your business need.
- If you are a security researcher, ast-grep can help you write rules much faster.
相關資源

ripgrep
CLI按正则递归搜索目录内容的高速文本搜索工具(命令名 rg),默认遵守 .gitignore 并自动跳过隐藏文件与二进制文件,支持按文件类型过滤,是编码 agent 检索代码库的常用命令。
BurntSushi/ripgrep69.2k
Codex CLI
CLIOpenAI 官方在本地运行的编码 Agent,Rust 实现,可读写代码、执行命令并在系统沙箱中运行;可用 ChatGPT 订阅登录,也可配置 API Key。
openai/codex130k
RTK (Rust Token Killer)
CLI在命令输出进入 agent 上下文之前做过滤和压缩的 Rust 命令行代理,覆盖 git、测试、grep、ls 等上百种命令,最多可减少约 90% 的 bash 输出;通过钩子自动改写 Claude Code、Codex 等 agent 执行的命令。
rtk-ai/rtk83.7k
Semgrep
CLI语义化的多语言静态分析工具:用写法接近源码的规则查找漏洞与缺陷,支持 30 多种语言与自定义规则,默认在本地扫描、不上传代码。跨文件分析与 Pro 规则需登录。
semgrep/semgrep17.1k
sd
CLI更直观的查找替换工具(sed 替代):使用 JavaScript / Python 风格的正则语法,支持字符串字面量模式与原地修改文件,适合 agent 批量改写文本。
chmln/sd7.4k
goose
CLI在本机运行的通用 AI Agent,提供桌面端、CLI 与 API,Rust 实现,可接入 15+ 家模型供应商并通过 MCP 连接 70+ 扩展;源自 Block,现属 Linux 基金会旗下 AAIF。
aaif-goose/goose55.4k