name: structural-search
Search code by its abstract syntax tree (AST) structure rather than plain text. Finds semantic patterns that regex cannot match reliably.
| Tool | Command | Use For |
|---|---|---|
| ast-grep | ast-grep -p 'pattern' |
AST-aware code search |
| sg | sg -p 'pattern' |
Short alias for ast-grep |
# Find all console.log calls
ast-grep -p 'console.log($_)'
# Find all function definitions
ast-grep -p 'function $NAME($_) { $$$ }'
# Find React useState hooks
ast-grep -p 'const [$_, $_] = useState($_)'
# Find Python function definitions
ast-grep -p 'def $NAME($_): $$$' --lang python
# Find all imports of a module
ast-grep -p 'import $_ from "react"'
# Search and show context
ast-grep -p 'fetch($_)' -A 3
# Search specific file types
ast-grep -p '$_.map($_)' --lang javascript
$NAME - matches single identifier$_ - matches any single node (wildcard)$$$ - matches zero or more nodes$$_ - matches one or more nodes