ast-grep CLI
Structural code search, lint and rewrite on the syntax tree, with patterns written like ordinary code
- 16,388 ★ on the repository
- Last commit
- MIT
Install
- Works in
In your terminal, with SkillFoxx CLI
npx skillfoxx add cli/ast-grepDetects the agents on your machine, shows the access level and pins the version.
Other ways to install
Access and checks
How we reviewExtended accessThe tool runs code or makes network calls and only reads external data.
- Rewrites project files in bulk when replacing
This is third-party code. Review the repository files before installing.
What it does
ast-grep searches code by syntax rather than text: a pattern is written as a code fragment, and metavariables like $A match any tree node. Matches can be rewritten with a replacement pattern right away. Lint and codemod rules are defined in YAML. Parsing uses tree-sitter, and the tool is written in Rust and runs on multiple cores. For an agent this means precise project-wide refactors instead of fragile regular expressions.
Who it is for. For developers and agents who need syntax-aware code search and bulk edits.
Good fit when
- You need to replace a deprecated API call across the project
- You want a custom lint rule for the team
- A regular expression over-matches or misses multi-line constructs
Not a fit when
- You need to replace plain text or config without language syntax
- You need type- and import-aware renames, where a language server fits better
Example request
Use ast-grep to replace every a && a() with a?.() in all TypeScript filesLimitations
Works only with languages that have a tree-sitter grammar. It has no type information, so constructs with the same shape but different meaning match alike. Patterns and YAML rules take some learning.
How to disable. Uninstall with the same package manager: npm uninstall -g @ast-grep/cli, pip uninstall ast-grep-cli or brew uninstall ast-grep.
FAQ
Why not grep or sed?
Matching follows code structure, so whitespace, line breaks and formatting do not matter, and strings or comments are not hit by accident.
Can I try it without installing?
Yes, the project website has an online playground.
Discussion
No questions or reviews yet.
Related
CLI toolsast-grep skills for Claude Code: the agent writes and tests structural AST search rules and builds a compact map of a codebase
Jevgrep
jevgrep
A CLI for coding agents: finds code by a task description rather than by a string. A key from one of the model providers is needed
A Rust binary for agents and the terminal that uses tree-sitter to find a symbol's definition, its calls and file dependencies
The zg local-first search tool: ripgrep, BM25 and vector search unified behind one interface for humans and agents
A code-review CLI and plugin for agents: reads a git diff and writes line-level comments with context
A Rust CLI for TypeScript and JavaScript: dead code, duplicates, cycles, complexity and architecture boundaries from one graph, plus MCP and skills
README in short
The README explains syntax-tree search and metavariable syntax. It installs via npm, pip, cargo, Homebrew, scoop, MacPorts, Nix and mise. It shows TypeScript rewrite examples and lists key features: code-like patterns, a tree traversal API and YAML rules. MIT licensed.