ast-grep

Search and rewrite code using AST patterns with metavariables.

14|1|Updated Mar 9, 2026
One-click install
npx skills add https://github.com/CodeAtCode/oss-ai-skills --skill ast-grep-codeatcode
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ast-grep
Source: https://github.com/CodeAtCode/oss-ai-skills/tree/main/tool/ast-grep
Command: npx skills add https://github.com/CodeAtCode/oss-ai-skills --skill ast-grep-codeatcode

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Efficiently locate and refactor code by leveraging AST patterns instead of brittle text searches, reducing false positives and manual effort.

Core Features & Use Cases

  • AST-pattern based search across languages to identify code patterns
  • Automated refactoring and code rewriting with metavariables for safe transformations
  • Structural linting and code discovery across large repos
  • Debugging aids to visualize AST/CST structures
  • Relational pattern matching like inside/has/precedes to constrain results

Quick Start

Install ast-grep and run a pattern search on your codebase to begin discovering matches.

Frequently Asked Questions about ast-grep

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

FAQPage Schema
How do I search for code patterns using AST instead of text matching?

AST-based code search finds structural code patterns instead of brittle text matches, reducing false positives. You write AST pattern rules with metavariables to locate exact code structures across a repository for safe code discovery.

How do I safely refactor code across multiple programming languages?

Automated refactoring across multi-language repositories is done using AST pattern rules with metavariables for safe code transformations. This structural approach rewrites code accurately without relying on error-prone text replacements.

What is structural linting and how does it improve static analysis?

Structural linting applies AST pattern matching to enforce code rules during static analysis. By analyzing the syntax tree, it identifies non-compliant structures across large codebases more reliably than regex-based linting.

Can I constrain AST search results based on code relationships?

Yes, you can constrain AST search results using relational pattern matching. Rules can specify structural constraints like inside, has, or precedes to filter matches based on their relationship to surrounding code.

Do I need a specific environment to run AST-based code search?

You need a Rust-based binary or a prebuilt release to run AST-based code search and refactoring. Once installed, you can execute pattern searches directly on your multi-language codebase from the CLI.

What is the best way to debug AST pattern matching rules?

Debug AST pattern matching rules using built-in debugging aids to visualize AST and CST structures. Inspecting the concrete syntax tree helps you verify that your metavariables and patterns align with the actual code structure.