ast-grep
A fast CLI tool for structural code search, linting, and rewriting based on abstract syntax trees.
At a Glance
MIT-licensed ast-grep CLI tool for code structural search, lint and rewriting.
Engagement
Available On
Alternatives
Listed Oct 2026
About ast-grep
ast-grep (sg) is an open-source, MIT-licensed command-line tool written in Rust for searching, linting, and rewriting code by syntax structure rather than text. Patterns are written like ordinary code and matched against tree-sitter-produced syntax trees across many files. It also offers a Node.js binding for programmatic use.
What It Is
ast-grep works like a syntax-aware grep/sed. You write a code pattern, using wildcards such as $MATCH to stand for any single AST node, and the tool finds all code with the same syntactical structure. It can then rewrite the matches with a replacement pattern, either from the command line or interactively.
Search, Lint, and Rewrite Workflow
A one-line command can locate and modify code, for example rewriting $A && $A() to $A?.() in TypeScript. For linting, rules are defined in YAML configuration and run with ast-grep scan, producing formatted error reports. The project also documents an interactive codemod mode, a language server, and a testing tool for rules.
Programmatic Usage and Languages
The @ast-grep/napi package exposes syntax trees through jQuery-like traversal methods, with opt-in type safety. The site lists 20+ supported languages, and custom tree-sitter parsers can be loaded dynamically. A web playground lets users try patterns in the browser.
Installation and Intended Users
It can be installed through npm, pip, cargo, cargo-binstall, Homebrew, Scoop, MacPorts, mise, or built from source. The README says it can help library authors ship breaking-change migrations, tech leads enforce coding practices, and security researchers write rules faster.
Community Discussions
Be the first to start a conversation about ast-grep
Share your experience with ast-grep, ask questions, or help others learn from your insights.
Pricing
Open Source
MIT-licensed ast-grep CLI tool for code structural search, lint and rewriting.
- CLI for structural search, lint, and rewriting
- Node.js binding (@ast-grep/napi)
- Interactive codemod, language server and testing tool
- 20+ supported languages
- Install via npm, pip, cargo, homebrew, scoop, MacPorts or mise
Capabilities
Key Features
- Structural code search using AST patterns
- Code rewriting and codemods
- YAML-based lint rules via ast-grep scan
- Interactive mode
- Language server
- Rule testing tool
- Node.js binding (@ast-grep/napi)
- 20+ supported languages
- Custom tree-sitter language loading
- Online playground
- Parallel multi-core processing
