ast-grep

Search and rewrite code structurally across files using syntax-tree patterns.

Updated Jul 9, 2026
One-click install
npx skills add https://github.com/ByronWilliamsCPA/plugin --skill ast-grep-byronwilliamscpa
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: ast-grep
Source: https://github.com/ByronWilliamsCPA/plugin/tree/main/plugins/wff-code/skills/ast-grep
Command: npx skills add https://github.com/ByronWilliamsCPA/plugin --skill ast-grep-byronwilliamscpa

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Literal text search fails when you need to find or refactor code by its syntactic shape rather than exact strings, making large-scale codemods and pattern-based refactoring error-prone. ## Core Features & Use Cases - Structural Search: Match code patterns with meta-variables ($X, $$$) across languages like Python, TypeScript, Bash, Rust, and Go, regardless of formatting. - Multi-file Rewrites: Apply template-based rewrites with --rewrite, with dry-run by default and interactive confirmation for safe refactoring. - Relational YAML Rules: Express contextual constraints like inside, has, follows, and precedes in rule files for lint-style checks. - Use Case: Rename a keyword argument in every connect(host=..., port=...) call across a Python codebase, or find all bare except: clauses inside function definitions. ## Quick Start Ask the assistant to use ast-grep to find all calls to a specific function pattern in your source directory and rewrite them with a new signature.

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 structure instead of text?▼

Use ast-grep with a pattern containing meta-variables like `$X` for single nodes and `$$$` for variadic matches. Run `ast-grep --lang python --pattern 'foo($$$)' src/` to match calls regardless of formatting or argument count.

When should I use ast-grep instead of grep?▼

Use ast-grep when the query describes a code structure, such as matching all calls to a function or finding decorator applications. Use grep for literal strings, prose, config values, or comment text that could appear anywhere.

How do I rewrite code across multiple files with ast-grep?▼

Pass `--pattern` with a matching template and `--rewrite` with the replacement using the same meta-variables. Rewrites are dry-run by default; add `--interactive` to confirm each change or `--update-all` to apply them.

Does ast-grep support matching nodes based on context?▼

Yes, YAML rule files support relational operators like `inside`, `has`, `follows`, and `precedes`, combined with `all`, `any`, and `not`. Pass the rule file with `--rule` to enforce contextual constraints.

Why does running sg not work for ast-grep?▼

The `sg` command is shadowed by shadow-utils, which ships its own `sg` as a `newgrp` wrapper. Always invoke the tool as `ast-grep` to avoid the conflict.

Does ast-grep respect gitignore files?▼

Yes, ast-grep respects `.gitignore` by default when searching directories. Pass `--no-ignore` to include ignored files in the search scope.