ast-grep

Search and rewrite code by matching AST structure across 25 languages.

Updated Mar 24, 2026
One-click install
npx skills add https://github.com/CNife/skills --skill ast-grep-cnife
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ast-grep
Source: https://github.com/CNife/skills/tree/main/.archive/ast-grep
Command: npx skills add https://github.com/CNife/skills --skill ast-grep-cnife

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This skill finds and rewrites code based on syntax tree structure, so you can target calls, imports, functions, and other code shapes without relying on fragile text matching.

Core Features & Use Cases

  • Structural search across 25 languages for calls, declarations, control flow, and other AST patterns.
  • Safe rewrite workflows with offline pattern validation, dry-run previews, and two-pass application when changes are confirmed.
  • Cross-platform binary installation and resolution for macOS, Linux, Windows, WSL, and Git Bash.
  • Useful for codemods, API migrations, log cleanup, lint-style scans, and repository-wide refactors.

Quick Start

Ask the assistant to search or rewrite the target code with ast-grep by giving the pattern, language, and file paths.

Frequently Asked Questions about ast-grep

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

FAQPage Schema
How do I rewrite code across an entire repository without fragile text matching?

Structural search matches AST shapes like calls and declarations instead of raw text. It applies to 25 languages, enabling repository-wide refactors, codemods, and API migrations without fragile regex or text matching.

What is the best way to automate API migrations and log cleanup safely?

Codemods for API migrations and log cleanup run safely by validating syntax tree patterns offline first. You preview changes in a dry-run, then execute a two-pass rewrite only when confirmed, preventing unintended modifications across the codebase.

Does structural search work for multiple programming languages or just one?

Structural search supports 25 languages for matching AST patterns like calls, declarations, and control flow. You specify the target language alongside your syntax tree pattern and file paths to find code shapes across different codebases.

Can I preview code changes before applying a repository-wide refactor?

Yes, you can preview changes using dry-run previews before applying rewrites. The workflow requires offline pattern validation first, then executes a two-pass application to apply the structural changes only when they are confirmed.

Do I need specific syntax tree patterns to run lint-style scans with ast-grep?

Yes, lint-style scans require valid syntax-tree patterns to match AST structure accurately. You provide the pattern, language, and file paths so the tool can identify specific code shapes like calls, imports, and functions.

Why does text matching fail for codemods compared to structural search?

Text matching fails because it cannot understand code structure, leading to broken syntax during rewrites. Structural search targets AST shapes directly, ensuring accurate modifications for API migrations and repository-wide refactors across multiple languages.