pbdeuchler/llm-plugins/howto-ast-grep
Claude Code and agent skill by pbdeuchler/llm-plugins
- community
Checking that the SKILL.md file is still in the repository.
Use when searching for or transforming code patterns structurally - provides ast-grep CLI syntax, metavariable patterns, and language-specific examples for precise AST-based code search and rewriting
Install
npx vskill@latest install pbdeuchler/llm-plugins/howto-ast-grepSKILL.md
The same SKILL.md was published earlier as pbdeuchler/llm-plugins/howto-ast-grep.
Using ast-grep for Structural Code Search
Overview
ast-grep (sg) matches code by AST structure, not text. Use it instead of grep/ripgrep when you need to match code patterns regardless of whitespace, variable names, or formatting. Patterns must be valid, parsable code in the target language.
When to Use
Prefer ast-grep over grep when:
- Finding function calls with specific argument shapes
- Matching patterns where variable names differ but structure is the same
- Rewriting code patterns across a codebase (migrations, deprecations)
- Finding anti-patterns that regex can't express (e.g. "await inside a loop")
Use grep instead when:
- Searching for string literals, comments, or non-code text
- Simple keyword search (import names, error messages)
- ast-grep is not installed
CLI Quick Reference
Metavariable Syntax
| Syntax | Matches | Example | |
|---|---|---|---|
$NAME | Exactly one AST node | console.log($MSG) matches console.log("hi") but not console.log("hi", 2) | |
$$$NAME | Zero or more nodes | console.log($$$ARGS) matches any number of arguments | |
$_NAME | One node, non-capturing | $_OBJ.$METHOD() matches without binding $_OBJ |
Reuse enforces equality: $A == $A matches x == x but not x == y.
Language-Specific Patterns
Always pass -l <language>. The language determines how patterns are parsed.
JavaScript/TypeScript:
Python:
Go:
Rust:
Pattern Pitfalls
| Mistake | Why It Fails | Fix | |
|---|---|---|---|
Using regex in -p | Patterns must be valid code | Write the code structure you want to find | |
$VAR + Name | Parsed as $VARN + ame | Use constraints or transform in YAML rules | |
| Single quotes in C/Rust patterns | 'x' is a char literal, not a string | Use "x" for strings | |
Forgetting -l flag | ast-grep cannot infer language from patterns | Always specify language explicitly | |
$A($B, $C) for variadic calls | Only matches exactly 2 args | Use $A($$$ARGS) for any arity |
YAML Rules for Complex Matching
When CLI patterns aren't expressive enough, use YAML rules:
Key rule combinators:
all: All sub-rules must match (AND)any: Any sub-rule can match (OR)not: Sub-rule must NOT matchinside: Target is inside another nodehas: Target contains a descendantfollows/precedes: Sibling ordering
Run rules: sg scan --rule path/to/rule.yml src/
Red Flags
- Using grep with complex regex when ast-grep would match the structure directly
- Forgetting
$$$and missing variadic matches - Writing patterns that aren't valid code in the target language
- Not specifying
-land getting parse failures
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingplugins/tooling/skills/howto-ast-grep/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/pbdeuchler/llm-plugins/howto-ast-grep)