ast-grep
Searches and rewrites code by AST shape across 25 languages
Install / Use
npx skills add code-yeongyu/oh-my-openagent --skill ast-grepInstalls into whichever agent you are using.
SKILL.md
Installable skill definition
Quality Score
Category
Development & EngineeringSupported Platforms
Tags
Our assessment of ast-grep
ast-grep scores 93/100 on our quality scale, 579th of 4,648 Development & Engineering skills we index (top 13%).
Its SKILL.md is 13 KB long, well organised into 37 sections with 8 code examples: a thorough specification that gives an agent plenty to work with.
With 69,362 GitHub stars, it is one of the more widely adopted skills in the catalogue.
Maintenance, license and trust
- The repository was last updated about 2 months ago, so ast-grep is actively maintained.
- Our last check on 2026-10-25 found the source still online.
- No license is declared. By default that means all rights are reserved: you can read it, but reusing or redistributing it is not clearly permitted. Ask the author before building on it commercially.
- Its trust signals score 88/100, with 1 caution from licensing, adoption, age or documentation. These come from repository metadata, not a code audit — read the skill file before letting an agent act on it.
Safety scan
No issues foundOur scan of the whole file found no instruction hijacking, hidden characters, credential access, data exfiltration or destructive commands. An AI review of the same text found nothing harmful.
AI review by kimi-k2.7-code on 2026-09-26. Automated pattern scan on 2026-09-26. It catches known dangerous patterns, not every risk — read a skill before letting an agent act on it.
ast-grep compared with similar skills
All 4 of these similar skills score higher than ast-grep; compare them before choosing.
| Skill | Score | Stars | Updated | Format |
|---|---|---|---|---|
| ast-grep (this skill)by code-yeongyu | 93 | 69.4k | 45d ago | SKILL.md |
| taste-skillby Leonxlnx | 100 | 112.0k | 1d ago | CLAUDE.md |
| ai-job-searchby ai-job-search-dev | 100 | 70.5k | today | CLAUDE.md |
| claude-howtoby luongnv89 | 100 | 46.7k | 5d ago | CLAUDE.md |
| lobehubby lobehub | 100 | 84.4k | today | MCP Server |
Frequently asked questions
- How do I install ast-grep?
- Run
npx skills add code-yeongyu/oh-my-openagent --skill ast-grep. The install tabs above show the steps for each supported agent. - Which AI agents does ast-grep work with?
- It is written for Universal, as a SKILL.md file. Other agents that read the same format can often use it too.
- Is ast-grep safe to use?
- Our scan of the whole file found no instruction hijacking, hidden characters, credential access, data exfiltration or destructive commands. An AI review of the same text found nothing harmful. It declares no license and scores 88/100 on trust signals. Skills are instructions an agent will follow, so read the file before installing it and do not approve commands you do not understand.
- Is ast-grep still maintained?
- The repository was last updated about 2 months ago, so ast-grep is actively maintained.
Skill content
View source on GitHubname: ast-grep description: "Searches and rewrites code by AST shape across 25 languages. Use when the target is a syntax pattern (every call/class/import shaped like X, a codemod, a YAML rule) rather than literal text; for plain strings, comments, or filenames, use rg."
ast-grep
sg (also installed as ast-grep) is an AST-aware search and rewrite tool across 25 languages. It treats your pattern as code, parses it the same way it parses your project, and matches structurally. It is the right tool whenever your question depends on code shape rather than text bytes.
This skill ships a Python wrapper at scripts/ast_grep_helper.py and platform install scripts at install.sh (POSIX) and install.ps1 (Windows). The helper adds offline pattern validation, the two-pass write trick, and binary auto-resolution. Use it as your default entry point.
OmO native: the ast-grep MCP tools (fastest path, no CLI needed)
OmO native also registers a bundled ast-grep MCP server. Its three tools are deferred: they are absent from the resident tool list and activate automatically the first time you call them by name — no tool_search round trip needed.
mcp__ast_grep_search({ pattern, language, paths })— AST-shape search, the MCP equivalent ofsg run -p. Prefer this over shelling out tosgfor one-off lookups: no install, no PATH issues.mcp__ast_grep_rewrite({ pattern, rewrite, language, paths, apply })— preview matches (apply: false, the default) or apply the rewrite (apply: true). Same two-pass discipline assg run -r: preview first, then apply.mcp__ast_grep_scan({ paths })— run project YAML rules (sgconfig.yml,rules/), the MCP equivalent ofsg scan.
Same pattern language as the CLI: $VAR matches one AST node, $$$VAR matches zero or more, and the pattern is code, not regex. paths accepts a file or directory list; language is required for patterns that are ambiguous across languages.
When to use this skill
Use it whenever the user's question is about code structure, not bytes:
- "Find every function that takes a
Requestparameter." - "Rewrite every
console.log(x)tologger.info(x)." - "Strip every
as anycast." - "Replace
require(...)withimportacross the repo." - "Find empty catch blocks."
- "Migrate
Optional[X]toX | None." - "Apply this codemod across these 200 files."
- "Run our YAML lint rules and surface violations."
Switch to plain grep / rg when the question is text-shaped (string literal contents, comments, license headers, file names, cross-language regex). When in doubt, ask: "does the answer depend on the language's syntax tree, or just on the file's bytes?" If the former, ast-grep. If the latter, grep.
Three things the agent must internalize
1. ast-grep is NOT regex
The wildcards are $VAR (one AST node) and $$$ (zero or more nodes). Regex syntax fails silently:
| You wrote | What ast-grep saw | What you wanted |
|---|---|---|
| foo\|bar | bitwise-or of foo and bar | run two separate searches |
| .*foo | not parseable | $$$ foo (if $$$ is a list of nodes) or use rg |
| \w+ | not parseable | $VAR to capture any identifier |
| [a-z] | character class, not parseable | switch to rg |
The full anti-pattern table is in references/pitfalls.md §1. The helper's validate subcommand catches these mechanically — call it before debugging "no matches" by hand.
2. Patterns must be valid code
The pattern itself must parse. def $FN($$$): fails because the trailing : makes it incomplete; use def $FN($$$). function $NAME without params/body fails; use function $NAME($$$) { $$$ }. Full table per language in references/pitfalls.md §2.
3. --update-all and --json are mutually exclusive (silently)
This is the single biggest gotcha when scripting. sg run -p P -r R --json --update-all returns the JSON but does not mutate files. To both preview AND apply, run two passes:
sg run -p P -r R --json=compact . # pass 1: see what would change
sg run -p P -r R --update-all . # pass 2: actually apply
The helper does this automatically when you call replace --apply. Read references/pitfalls.md §9.
The helper script — scripts/ast_grep_helper.py
A single-file Python 3 stdlib wrapper. Same on every OS. The agent's default entry point.
search — find all matches of a pattern
python3 scripts/ast_grep_helper.py search 'console.log($MSG)' --lang ts src/
Validates the pattern offline first. If the pattern looks like regex (\w, .*, |, etc.) the helper exits with a hint and never calls sg — saves a round-trip. Pass --force to skip validation.
Flags:
--lang ts(or any of the 25 languages; aliases likejs,py,rs,ktaccepted)--globs '!**/*.test.ts'(repeatable; prefix!to exclude)-C 3(context lines)--json-out(raw JSON instead of human format)
replace — rewrite by pattern, dry-run by default
# Dry-run preview (default — no files mutated)
python3 scripts/ast_grep_helper.py replace 'console.log($MSG)' 'logger.info($MSG)' --lang ts src/
# Actually apply
python3 scripts/ast_grep_helper.py replace 'console.log($MSG)' 'logger.info($MSG)' --lang ts src/ --apply
The helper:
- Validates both
patternandrewritefor hint-detectable mistakes. - Runs pass 1 with
--json=compactto collect matches and show a preview. - If
--applyis set, runs pass 2 with--update-allto mutate files.
scan — run YAML rules
# Discover sgconfig.yml from cwd and run all rules
python3 scripts/ast_grep_helper.py scan src/
# Run a single rule file
python3 scripts/ast_grep_helper.py scan -r rules/no-console.yml src/
# Apply auto-fixes
python3 scripts/ast_grep_helper.py scan -U src/
# CI-friendly GitHub annotations
python3 scripts/ast_grep_helper.py scan --report-style short src/
validate — offline pattern check (no sg call)
Useful for CI lints, pre-commit hooks, and quick sanity checks:
python3 scripts/ast_grep_helper.py validate '\w+' --lang ts
# → exit 2: regex \w not supported. Use $VAR for identifiers.
python3 scripts/ast_grep_helper.py validate 'console.log($MSG)' --lang ts
# → exit 0: pattern looks plausible for ast-grep.
langs / doctor / install
python3 scripts/ast_grep_helper.py langs # list 25 supported languages and aliases
python3 scripts/ast_grep_helper.py doctor # check ast-grep binary availability
python3 scripts/ast_grep_helper.py install # delegate to install.sh / install.ps1
new and test subcommands proxy directly to sg new and sg test.
Direct sg use (when the helper isn't enough)
The helper is opinionated. For full control, drop to sg. The skill ships a CLI cheat sheet in references/cli.md. The minimal idioms:
# Search
sg run -p 'console.log($MSG)' --lang ts src/
# Search with JSON for scripting
sg run -p 'console.log($MSG)' --lang ts --json=compact src/ | jq '.[] | .file'
# Rewrite, dry-run
sg run -p 'console.log($MSG)' -r 'logger.info($MSG)' --lang ts --json=compact src/
# Rewrite, apply
sg run -p 'console.log($MSG)' -r 'logger.info($MSG)' --lang ts --update-all src/
# Pattern from stdin (great for ad-hoc experiments)
echo 'console.log("hi")' | sg run -p 'console.log($MSG)' --lang js --stdin
# Debug a pattern that returns 0 matches
sg run -p '<your pattern>' --lang <lang> --debug-query=ast --stdin <<< '<sample-code>'
# Run YAML rules
sg scan src/
# Inline YAML rule (one-off)
sg scan --inline-rules '
id: no-todo
language: TypeScript
severity: warning
rule: { pattern: TODO }' src/
When using sg directly in a shell, always single-quote patterns so $VAR is not expanded by the shell.
Decision tree — what to use, when
USER asks for "find/rewrite/codemod"
│
├─ structural pattern (function shape, call, class, import, control flow)
│ └→ ast-grep (this skill)
│
├─ text pattern (regex, alternation, character classes, file names)
│ └→ rg / grep
│
├─ semantic question (what variable does this refer to? does this throw?)
│ └→ LSP tools, TypeScript compiler, Pyright, Semgrep with type inference
│
└─ multiple repos / federated search
└→ a search engine + then ast-grep / rg / LSP per-repo
If the user says "find all" or "every", default to ast-grep when the target is shaped (function, class, call, import, statement). Default to rg when the target is text (string content, comment, license header, file name, identifier substring).
Always run dry-run first when rewriting
A bad pattern silently rewrites the wrong thing. The helper's replace defaults to dry-run for this reason. The flow is:
- Search to confirm matches:
helper search '<pattern>' --lang X . - Dry-run rewrite:
helper replace '<pattern>' '<rewrite>' --lang X .(no--apply) - Inspect the dry-run summary: number of matches, files affected, the per-location preview.
- If wrong: refine pattern, go back to step 1.
- If right:
helper replace '<pattern>' '<rewrite>' --lang X . --apply.
Never apply a rewrite that you have not first dry-run.
When sg returns 0 matches but you know the code is there
In priority order:
- Run
helper validate '<pattern>' --lang <lang>— catches regex misuse, missing function bodies, Python trailing colons. - Check
--lang—sginfers from extension; if you pass a.tsxfile with--lang ts(nottsx), JSX won't parse. - Inspect the parsed pattern:
sg run -p '<pattern>' --lang <lang> --debug-query=ast --stdin <<< '<sample>'. If it showsERRORnodes, the pattern is malformed. - Check the AST of the target file:
sg run -p '$_' --lang <lang> --debug-query=cst path/to/file | head -40— find thekindyou're trying to match. - Try the playground: https://ast-grep.github.io/playground.html — paste code + pattern, see what's happening.
Do not blindly retry with variations. Each failure has a reason; surface it.
When to use YAML rules vs inline -p patterns
Use inline -p when:
- One-off ad-hoc query.
- The pattern is simple (no constraints, no fix template).
- You're exploring.
Use YAML rules (file under rules/, run via sg scan) when:
- The pattern is reused (lint rule, codemod that runs in CI).
- You need
constraints,transform, complexinside/has, or composite logic. - You want auto-fix (
fix:field). - You want to test the rule (snapshot tests via
sg test).
The full YAML rule schema is in references/yaml-rules.md. Project setup (sgconfig.yml, ruleDirs, utilDirs) is in references/sgconfig.md.
Output discipline
sg run --json=compactproduces an array of match objects:{ file, range: {start, end}, text, replacement?, lines, language, ... }. Pipe throughjqfor further processing.- Without
--json,sgproduces human-readable colored output suitable for terminals. - The helper's default output is human-readable (file:line:column + match preview). Pass
--json-outfor raw JSON. - The helper's
replacealways summarizes: number of matches, number of files, per-location preview.
When summarizing for the user, always include the count of files affected, not just the count of matches. Users care about blast radius.
Required reading (in order of priority)
references/patterns.md— meta-variables, naming rules, strictness levels. Read when you're unsure why a pattern doesn't match.references/pitfalls.md— the failure-mode field guide. Read when 0 matches surprises you.references/recipes.md— copy-paste patterns by language. Read first when you start a new task.references/cli.md—sg run,sg scan,sg test,sg new,sg lsp. Read when the helper isn't enough.references/yaml-rules.md— YAML rule schema. Read when you outgrow inline patterns.references/sgconfig.md— project-level configuration. Read when
Truncated for display — read the full file on GitHub.
Related Skills
taste-skill
112.0kAgent skills that give coding agents taste: art direction, reference images and a browser check instead of generic AI pages
ai-job-search
70.5kThe job search that runs on your machine. AI job application framework built on Claude Code: evaluate postings, tailor CVs, write cover letters, prep interviews. Fork it and own it.
claude-howto
46.7kA visual, example-driven guide to Claude Code — from basic concepts to advanced agents, with copy-paste templates that bring immediate value.
lobehub
84.4k🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
Languages
Trust signals
From repository metadata: license, adoption, age and documentation. Not a code audit — see the Safety scan above for what the skill file itself contains.
