ast-grep
Use ast-grep (sg) for AST-aware code search and rewrite across 25 languages. Trigger for structural code matching or deterministic codemods: find every function/call/class/import shaped like X, rewrite console.log to logger.info, strip `as any`, migrate require() to import, find empty catch blocks or missing await, and scan/apply YAML rules. Prefer this over rg/grep when the target is syntax shape rather than text; use rg for string contents, comments, filenames, or regex-style byte searches.
npx skills add code-yeongyu/oh-my-openagent --skill ast-grep --agent claude-code
Same command for any agent — swap --agent for codex, cursor, copilot.
Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.
# 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. --- ## When to use this skill Use it whenever the user's question is about **code structure**, not bytes: - "Find every function that takes a `Request` parameter." - "Rewrite every `console.log(x)` to `logger.info(x)`." - "Strip every `as any` cast." - "Replace `require(...)` with `import` across the repo." - "Find empty catch blocks." - "Migrate `Optional[X]` to `X | 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, c
- When to use this skill
- Three things the agent must internalize
- 1. ast-grep is NOT regex
- 2. Patterns must be valid code
- 3. --update-all and --json are mutually exclusive (silently)
- The helper script — scripts/astgrephelper.py
- search — find all matches of a pattern
- replace — rewrite by pattern, dry-run by default
- scan — run YAML rules
- validate — offline pattern check (no sg call)
- langs / doctor / install
- Direct sg use (when the helper isn't enough)
- Decision tree — what to use, when
- Always run dry-run first when rewriting
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 python3 scripts/ast_grep_helper.py search 'console.log($MSG)' --lang ts src/ 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 Discover sgconfig.yml from cwd and run all rules python3 scripts/ast_grep_helper.py scan src/ Run a single rule file
What does the ast-grep skill do?
Use ast-grep (sg) for AST-aware code search and rewrite across 25 languages. Trigger for structural code matching or deterministic codemods: find every function/call/class/import shaped like X, rewrite console.log to logger.info, strip `as any`, migrate require() to import, find empty catch blocks or missing await, and scan/apply YAML rules. Prefer this over rg/grep when the target is syntax shape rather than text; use rg for string contents, comments, filenames, or regex-style byte searches.
How do I install it?
Run `npx skills add code-yeongyu/oh-my-openagent --skill ast-grep --agent claude-code` — it drops the skill into your project so the agent can pick it up. Swap the --agent value for codex, cursor or copilot if you use one of those.
Where does this skill come from?
From code-yeongyu/oh-my-openagent, a repository with 67,209 stars. We read it straight from the repository tree rather than a submitted listing, so what you see here is what is actually published.
Is a popular skill a good skill?
Not necessarily. Stars measure attention, not adoption — a repository can trend for a week and be abandoned. That is why we show the weekly change from our own snapshots next to the total, instead of a single flattering number.