ast-grep Structural Code Search & Rewrite
Search and rewrite code across 25 languages by syntax shape (AST) instead of text, with a safe two-pass codemod workflow.
Dev & CodingIntermediate★ 67,531⑂ 5,507AI score 9/10Last updated: Aug 9, 2026
What it does
Drives ast-grep (sg) to match and rewrite code structurally: $VAR matches one AST node, $$$ matches zero or more. Ideal when the question depends on code shape rather than bytes.
- Ships a stdlib-only Python wrapper (
scripts/ast_grep_helper.py) withsearch,replace,scan,validate,langs,doctor, andinstallsubcommands. - Validates patterns offline first, catching the regex-misuse mistakes (
\w,.*,|) that cause most "0 matches" sessions. - Handles ast-grep's biggest scripting gotcha automatically:
--jsonand--update-allare mutually exclusive, so it runs a preview pass then an apply pass. replaceis dry-run by default and always reports match count and number of files touched.- Supports YAML rules via
sg scanfor reusable lint rules, auto-fixes, and CI annotations. - Includes a decision tree for when to use ast-grep vs
rg/grep vs LSP-based tooling.
Who it's for
- Engineers running large migrations (require → import,
Optional[X]→X | None, API renames). - Anyone burned by grep false positives during refactors.
- Team leads adding custom structural lint rules to CI.
- Users who want Claude Code to "change them all" without silent damage.
Examples
- Logger migration: "Rewrite every
console.log(x)tologger.info(x)" → dry-run preview with blast radius, then--apply. - Type cleanup: "Strip all
as anycasts in TypeScript and list what's left" → structural matching skips strings and comments. - Codebase audit: "Find empty catch blocks and turn it into a YAML rule for CI" → author a rule in
rules/, runsg scan --report-style short.
· · · Install guide · · ·
Install in the Claude app (no terminal)
- Download the ZIP with the button below.
- In Claude, open Settings → Capabilities and turn on 'Code execution and file creation'. (one time)
- Go to Customize → Skills → + → 'Upload a skill' and upload the ZIP.
Install in Claude Code
Let Claude do it — paste this into Claude Code
Install the skill I found on Claude Skill Mart. Copy the packages/shared-skills/skills/ast-grep folder from the GitHub repo code-yeongyu/oh-my-openagent into my ~/.claude/skills/ast-grep/. When it's done, tell me in one line what this skill can do.
Install with a command instead
git clone https://github.com/code-yeongyu/oh-my-openagent.git && mkdir -p ~/.claude/skills && cp -r oh-my-openagent/packages/shared-skills/skills/ast-grep ~/.claude/skills/⚠ This is a third-party skill. Check the source repository before installing.
- Open a terminal and confirm Python 3 exists:
python3 --version. - Clone the repo:
git clone https://github.com/code-yeongyu/oh-my-openagent.git - Copy the skill:
mkdir -p ~/.claude/skills && cp -r oh-my-openagent/packages/shared-skills/skills/ast-grep ~/.claude/skills/ - Install the binary:
bash ~/.claude/skills/ast-grep/install.sh(Windows: runinstall.ps1in PowerShell), or usebrew install ast-grep/cargo install ast-grep. - Verify with
python3 ~/.claude/skills/ast-grep/scripts/ast_grep_helper.py doctor. - Restart Claude Code, then ask something like "find every function that takes a Request parameter".
- Before any rewrite, commit your work, review the dry-run output, and only then add
--apply. On Linux prefer theast-grepbinary name oversg.
View source on GitHub ↗License: NOASSERTION