ast-grep

Search and rewrite code structurally across 25 languages using AST patterns.

Updated Sep 10, 2026
One-click install
npx skills add https://github.com/loteiron/ZeusAgent --skill ast-grep-loteiron
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: ast-grep
Source: https://github.com/loteiron/ZeusAgent/tree/main/optional-skills/software-development/ast-grep
Command: npx skills add https://github.com/loteiron/ZeusAgent --skill ast-grep-loteiron

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve? Text-based search tools like grep cannot understand code structure, so finding every function with a specific signature or rewriting a call pattern across hundreds of files produces false positives and missed matches. This Skill performs AST-aware structural search and rewrite, matching code by its syntax tree rather than raw bytes. ## Core Features & Use Cases - Structural Search: Find code by AST pattern using meta-variables like $VAR and $$$ across TypeScript, Python, Go, Rust, Java, and 20 other languages. - Pattern-Driven Rewrites: Preview codemods with dry-run output, then apply bulk rewrites with a two-pass flow that avoids the silent --update-all/--json conflict. - YAML Rule Scanning: Run reusable lint rules with constraints, auto-fixes, and CI-friendly report formats via sg scan. - Offline Pattern Validation: Catch regex misuse and malformed patterns before invoking the binary, saving debugging round-trips. - Use Case: Migrate every console.log(x) call to logger.info(x) across a 200-file TypeScript repo: validate the pattern, dry-run the rewrite to inspect the blast radius, then apply and review with git diff. ## Quick Start Ask the agent to find every console.log call in the src directory and rewrite them to logger.info using the ast-grep helper script.

Frequently Asked Questions about ast-grep

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I search code by AST pattern instead of regex?▼

Use ast-grep with patterns written in the target language syntax, where $VAR matches one AST node and $$$ matches zero or more nodes. Run the helper's search subcommand with --lang set, for example searching 'console.log($MSG)' across TypeScript files.

How to rewrite code across many files with ast-grep?▼

Run the helper's replace subcommand with a pattern and rewrite template; it defaults to a dry-run preview showing matches and affected files. Add --apply to mutate files, which internally runs a second pass with --update-all since sg silently ignores it when --json is set.

When should I use ast-grep vs grep or ripgrep?▼

Use ast-grep when the answer depends on code structure, such as function shapes, call patterns, imports, or control flow. Use ripgrep when the target is text-shaped, like string literal contents, comments, license headers, or cross-language regex with alternation.

Why does my ast-grep pattern return 0 matches?▼

Most failures come from regex syntax like \w, .*, or | which ast-grep does not support, or from incomplete patterns like a Python def with a trailing colon. Run the helper's validate subcommand offline, then inspect the parsed pattern with --debug-query=ast.

Does ast-grep work on Windows, macOS, and Linux?▼

Yes, the skill ships install.sh for POSIX systems and install.ps1 for Windows, trying brew, npm, cargo, pip, scoop, and winget before falling back to a pinned GitHub release binary. On Linux, prefer the ast-grep binary name since sg collides with the setgroups command.

What are the limitations of structural code search?▼

ast-grep is a structural matcher with no scope, type, or data-flow analysis, so it cannot tell whether two identifiers refer to the same variable or whether a function throws. For semantic questions, use LSP tools, Pyright, or Semgrep with type inference.