ast-grep: Structural Code Search and Editing
Use ast-grep to solve the "old_string not unique" problem by matching code structure instead of exact text. This enables refactoring across formatting variations and structural patterns.
When to Use ast-grep vs Text Tools
Use ast-grep when:
- Structural code changes - Refactoring function signatures, method calls, class structures
- Formatting-independent matching - Need to find code regardless of whitespace/line breaks
- Pattern variations - Matching similar structures with different variable names/arguments
- "old_string not unique" problem - Edit tool fails because text appears in multiple contexts
- Complex queries - Finding nested structures, specific AST patterns
Use text tools (Edit/Grep) when:
- Simple, unique string replacement - The exact text appears once or in consistent format
- Non-code files - Markdown, configs, data files
- Comment/documentation edits - Content that isn't code structure
- Very small changes - Single line, obvious context, no ambiguity
Key Decision Rule
If editing code structure and there's any ambiguity in text matching → use ast-grep.
ast-grep's primary value: Solves the "old_string not unique" problem by matching structure instead of exact text.
Detailed Reference
For comprehensive patterns, syntax, metavariables, common use cases, language-specific tips, and best practices, load ast-grep guide.
The reference includes:
- Pattern syntax and metavariables (
$VAR, $$$ARGS, $$STMT)
- Recommended workflow (search, verify, apply, validate)
- Common use cases with examples (function calls, imports, method renames)
- Language-specific tips (JavaScript/TypeScript, Python, Go, Rust)
- Best practices and pitfalls to avoid
- Integration strategies with Edit tool
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: ast-grep3description: Use ast-grep for structural code search and refactoring when editing code structure with ambiguity in text matching, handling "old_string not unique" problems, or performing formatting-independent pattern matching across function signatures, method calls, and class structures Use when this capability is needed.4---56# ast-grep: Structural Code Search and Editing78Use ast-grep to solve the "old_string not unique" problem by matching code structure instead of exact text. This enables refactoring across formatting variations and structural patterns.910## When to Use ast-grep vs Text Tools1112### Use ast-grep when:13- **Structural code changes** - Refactoring function signatures, method calls, class structures14- **Formatting-independent matching** - Need to find code regardless of whitespace/line breaks15- **Pattern variations** - Matching similar structures with different variable names/arguments16- **"old_string not unique" problem** - Edit tool fails because text appears in multiple contexts17- **Complex queries** - Finding nested structures, specific AST patterns1819### Use text tools (Edit/Grep) when:20- **Simple, unique string replacement** - The exact text appears once or in consistent format21- **Non-code files** - Markdown, configs, data files22- **Comment/documentation edits** - Content that isn't code structure23- **Very small changes** - Single line, obvious context, no ambiguity2425## Key Decision Rule2627**If editing code structure and there's any ambiguity in text matching → use ast-grep.**2829ast-grep's primary value: **Solves the "old_string not unique" problem by matching structure instead of exact text.**3031## Detailed Reference3233For comprehensive patterns, syntax, metavariables, common use cases, language-specific tips, and best practices, load [ast-grep guide](./reference/ast-grep-guide.md).3435The reference includes:36- Pattern syntax and metavariables (`$VAR`, `$$$ARGS`, `$$STMT`)37- Recommended workflow (search, verify, apply, validate)38- Common use cases with examples (function calls, imports, method renames)39- Language-specific tips (JavaScript/TypeScript, Python, Go, Rust)40- Best practices and pitfalls to avoid41- Integration strategies with Edit tool4243---44> Converted and distributed by [TomeVault](https://tomevault.io/claim/xdg) — claim your Tome and manage your conversions.45<!-- tomevault:4.0:skill_md:2026-04-11 -->