ast-grep
Was er macht
Dieser Skill hilft deinem Agenten, Code anhand seines Aufbaus zu suchen und zu ändern, nicht nur anhand von Textübereinstimmungen. Er ist für Entwickler gedacht, die zuverlässige Änderungen über den gesamten Code brauchen.
Wann du ihn nutzt
- ✓Wenn du Code mit einer bestimmten Struktur finden willst, etwa jede Funktion mit einem bestimmten Parameter.
- ✓Wenn du ein Muster über viele Dateien umschreiben willst, etwa einen Logging-Aufruf durch einen anderen ersetzen.
- ✓Wenn du eine Codebasis migrieren oder Lint-artige Regeln über ein Projekt laufen lassen musst.
Link zum Skill
https://github.com/NousResearch/hermes-agent/blob/main/optional-skills/software-development/ast-grep/SKILL.mdKopiere ihn und füge ihn in deine KI ein (Claude, ChatGPT oder Cursor) mit der Bitte, den Anweisungen zu folgen.
So nutzt du ihn
Am einfachsten: Kopiere die Nachricht und füge sie in deine KI ein. Sie liest den Skill und folgt den Anweisungen.
Fertige Nachricht zum Einfügen
Für Claude Code, Codex oder Cursor: Führe den Befehl in deinem Projektordner aus, dann wird der Skill installiert.
Lade die SKILL.md herunter und lege sie in den Skills-Ordner deines Agenten.
Beispielanfrage an deine KI
“Finde jede Stelle in diesem Projekt, an der console.log aufgerufen wird, und ersetze sie durch logger.info.”
5 neue Skills pro Woche. 1 Minute Lesezeit.
So funktioniert es
Der Skill nutzt ast-grep, das Code so liest, wie es ein Compiler tut, und Muster nach ihrer Form erkennt. Dazu gehören ein Hilfsskript und Installationsskripte für Windows und andere Systeme.
Technischer Inhalt (originale SKILL.md)
Originaltext des Autors, auf Englisch, direkt von GitHub.
ast-grep
ast-grep (binary also named sg) 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.
Upstream source: vendored from code-yeongyu/ast-grep-skill (MIT), as shipped in oh-my-openagent's shared-skills bundle.
When to use this skill
Use it whenever the 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 search_files (or plain 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, search_files.
Hermes integration notes:
- Run the helper and
sgthrough theterminaltool. Single-quote every pattern so the shell never expands$VAR. - For find→read chains around matches, use
--json-outand process withexecute_coderather than piping through interpreters. - This complements (does not replace) Hermes's
patchtool:patchis for targeted edits you author; ast-grep is for pattern-driven bulk rewrites across many sites.
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
python 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)
python scripts/ast_grep_helper.py replace 'console.log($MSG)' 'logger.info($MSG)' --lang ts src/
# Actually apply
python scripts/ast_grep_helper.py replace 'console.log($MSG
Häufige Fragen
Wann sollte ich stattdessen eine einfache Textsuche nutzen?
Der Skill sagt, Textsuche eignet sich besser für Zeichenketten, Kommentare, Dateinamen und sprachübergreifende Regex.
Ändert es Dateien automatisch?
Der Skill nennt ein Schreibverfahren in zwei Durchgängen, sodass Änderungen geprüft werden, bevor sie übernommen werden.
Welche Sprachen werden unterstützt?
Der Skill sagt, ast-grep deckt 25 Sprachen ab.
Details
Originalbeschreibung des Autors
AST-aware structural code search and rewrite via ast-grep.
Beliebtheit
- GitHub-Sterne
- 260k
- Platz im Ranking
- #569
- Lizenz
- MIT
- Aktualisiert
- 2.11.2026
Kommentare
- …
Weitere Skills aus diesem Repository
agent-merge-conflict-arbiter
260kLöst einen Git-Merge-Konflikt zwischen den Branches zweier Agenten als neutrale dritte Partei und erzeugt ein zusammengeführtes Ergebnis für das Repo.
antigravity-cli
260kFührt dich durch Installation, Aktualisierung und Fehlersuche des Antigravity-Kommandozeilenwerkzeugs, einschließlich Plugins, Anmeldung und Sandbox.
blackbox
260kLässt deinen Agenten eine Coding-Aufgabe an das Blackbox-KI-Kommandozeilenwerkzeug übergeben, das mehrere KI-Modelle nutzt und das beste Ergebnis auswählt.
dynamic-workflow
260kBringt deinem Agenten bei, große Stapel gleichartiger Arbeit als gespeichertes Skript auszuführen, sodass nur geprüfte Ergebnisse in die Hauptunterhaltung zurückkehren.
Ähnliche Skills
diffs
394kLässt deinen Agenten einen echten, teilbaren Diff seiner Änderungen als Viewer-Link oder Bilddatei erstellen, statt einer handgeschriebenen Zusammenfassung.
coding-agent
394kHilft deinem Agenten, eine größere Programmieraufgabe als Hintergrund-Worker an Codex, Claude Code oder OpenCode zu übergeben und danach den Fortschritt zu prüfen.
gh-issues
394kHilft deinem Agenten, GitHub-Issues zu finden, die zu bearbeitenden auszuwählen und Pull Requests mit Korrekturen zu öffnen, einschließlich der Nacharbeit an Review-Kommentaren.