ast-grep
Structural code search using ast-grep. Use for syntax-aware searches, finding patterns, refactoring, or when you need to understand code structure beyond plain text matching.
Installation
Follow the repository instructions for this skill and your agent. Review the files and dependencies before adding it to your workflow.
View source ↗GuidesSkill instructions
ast-grep
A CLI tool for structural code search using AST patterns.
When to Use
Use ast-grep instead of text-based search (grep, ripgrep) when:
- You need syntax-aware matching (not just text)
- Searching for code patterns regardless of formatting
- Finding function definitions, calls, or specific constructs
- Refactoring or analyzing code structure
Basic Usage
ast-grep --lang <language> -p '<pattern>'
Supported Languages
Common languages: rust, typescript, javascript, python, go, c, cpp, java, kotlin, swift, ruby, lua, html, css
Pattern Syntax
Metavariables
-
$NAME- matches a single AST node (identifier, expression, etc.) -
$$$ARGS- matches zero or more nodes (for argument lists, etc.) -
$_- matches any single node (wildcard)
Examples
Find all function calls to a specific function:
ast-grep --lang rust -p 'println!($$$ARGS)'
Find all struct definitions:
ast-grep --lang rust -p 'struct $NAME { $$$FIELDS }'
Find all async functions:
ast-grep --lang rust -p 'async fn $NAME($$$ARGS) $BODY'
Find method calls on a type:
ast-grep --lang rust -p '$EXPR.unwrap()'
Find if-let patterns:
ast-grep --lang rust -p 'if let $PATTERN = $EXPR { $$$BODY }'
Find React component definitions (TypeScript):
ast-grep --lang typescript -p 'function $NAME($PROPS): JSX.Element { $$$BODY }'
Find Python class definitions:
ast-grep --lang python -p 'class $NAME: $$$BODY'
Useful Options
-
--json- Output results as JSON for parsing -
--color=never- Disable color (useful for piping) -
-i, --interactive- Interactive mode for reviewing matches -
--rewrite <pattern>- Replace matches with a new pattern
Tips
- Start with a simple pattern and refine
- Use
$_when you don't care about a specific part - Use
$$$for variadic matches (arguments, statements) - Test patterns interactively with
ast-grep --interactive
Examples
Read the usage examples in the source repository.
View source ↗Source history
- Indexed on
- 2026-01-20
- Index updated
- 2026-03-03
- Branch
- main
Index and sync dates describe this directory. View the source repository for code history.
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!