ast-grep

Search and rewrite code using AST-aware patterns across multiple programming languages.

Updated Nov 7, 2024
One-click install
npx skills add https://github.com/jeremysball/dotfiles --skill ast-grep-jeremysball
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ast-grep
Source: https://github.com/jeremysball/dotfiles/tree/main/.pi/skills-archive/ast-grep
Command: npx skills add https://github.com/jeremysball/dotfiles --skill ast-grep-jeremysball

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Text-based tools like sed and grep match raw strings, so they hit comments, string literals, and wrong scopes when modifying code. This Skill provides AST-aware search and replace patterns that understand code structure, making refactors precise and safe. ## Core Features & Use Cases - Structural Search: Find functions, calls, or patterns by syntax tree shape using metavariables like $NAME and $$$ARGS across 15+ languages. - Safe Rewrites: Preview and apply code transformations with pattern-based replacement that respects scope and syntax. - YAML Rule Files: Define reusable lint-style rules with constraints, context matching, and automatic fixes, then run them project-wide via ast-grep scan. - Use Case: Migrate an entire TypeScript codebase from an old API to a new one by matching call sites structurally and rewriting them in place, without touching comments or unrelated identifiers. ## Quick Start Ask the AI to use ast-grep to find all async functions in your TypeScript files or to rewrite every call to an old API with its replacement.

Frequently Asked Questions about ast-grep

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I search and replace code with ast-grep?

Run ast-grep with -p for the search pattern and -r for the replacement, plus -l for the language. Add --rewrite to apply changes in place; without it the command shows a dry-run preview of every match.

ast-grep vs sed for code refactoring, which should I use?

Use ast-grep for code transformations because it matches syntax tree nodes and respects scope, so it skips comments, strings, and unrelated identifiers. Reserve sed and grep for plain text without code structure, like logs or config files.

What languages does ast-grep support?

ast-grep supports JavaScript, TypeScript, Python, Rust, Go, Java, C, C++, Ruby, PHP, Swift, C#, HTML, CSS, Bash, and SQL. Select the language with the -l flag, such as -l ts for TypeScript.

How do I install ast-grep?

Install via Homebrew with brew install ast-grep, via npm with npm install -g @ast-grep/cli, via Cargo with cargo install ast-grep, or with the official curl install script on Linux.

When should I not use ast-grep?

Avoid ast-grep for non-code text such as logs, INI config files, or one-off string replacements without syntax context. Plain grep or sed is simpler and sufficient when no code structure is involved.