/tooling:howto-ast-grepfrom tooling
Provides ast-grep CLI syntax, metavariable patterns, and language-specific examples for precise AST-based code search and rewriting across multiple languages.
Ships in tooling. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
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.
Prefer ast-grep over grep when:
Use grep instead when:
# Search for a pattern
sg run -p 'console.log($$$ARGS)' -l javascript src/
# Search and replace (interactive)
sg run -p '$A && $A()' -r '$A?.()' -l typescript --interactive src/
# Apply all replacements without prompting
sg run -p 'var $X = $Y' -r 'const $X = $Y' -l javascript -U src/
# JSON output for programmatic use
sg run -p '$FUNC($$$)' -l python --json src/
# Read from stdin
echo 'let x = 1' | sg run -p 'let $X = $Y' -l javascript --stdin
| 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.
Always pass -l <language>. The language determines how patterns are parsed.
JavaScript/TypeScript:
# Find React hooks
sg run -p 'use$HOOK($$$)' -l typescript src/
# Optional chaining candidates
sg run -p '$A && $A.$B' -r '$A?.$B' -l typescript src/
# Async/await patterns
sg run -p 'await $PROMISE' -l javascript src/
Python:
# Find print statements
sg run -p 'print($$$)' -l python src/
# Dictionary access patterns
sg run -p '$DICT[$KEY]' -l python src/
# With statement patterns
sg run -p 'with open($$$ARGS) as $F: $$$BODY' -l python src/
Go:
# Error handling
sg run -p 'if $ERR != nil { return $$$VALS }' -l go ./
# Function calls
sg run -p '$PKG.$FUNC($$$)' -l go ./
Rust:
# Unwrap calls (potential panics)
sg run -p '$EXPR.unwrap()' -l rust src/
# Match expressions
sg run -p 'match $VAL { $$$ARMS }' -l rust src/
| 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 |
When CLI patterns aren't expressive enough, use YAML rules:
id: no-unwrap-in-production
language: rust
rule:
pattern: $EXPR.unwrap()
not:
inside:
kind: function_item
has:
field: name
regex: '^test_'
message: "Use expect() or proper error handling instead of unwrap()"
severity: warning
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 orderingRun rules: sg scan --rule path/to/rule.yml src/
$$$ and missing variadic matches-l and getting parse failuresWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub pbdeuchler/llm-plugins --plugin tooling3 skills in this plugin
/howto-qmdUse when searching markdown knowledge bases, documentation, or notes for relevant context - provides qmd CLI syntax for keyword, semantic, and hybrid search across indexed collections/howto-uvUse when running Python, installing Python packages, creating virtualenvs, or executing Python CLI tools - mandates uv/uvx over direct python/pip/pipx/venv calls when uv is installed, and gives the drop-in command mappingsOther plugins' skills for the same job