whatisgithub

What is ast-grep?

adsharma/ast-grep — explained in plain English

Analysis updated 2026-05-18

3RustAudience · developerComplexity · 3/5Setup · easy

In one sentence

A command line tool that searches and rewrites code by its structure instead of plain text, using patterns that look like ordinary code.

Mindmap

mindmap
  root((ast-grep))
    What it does
      Structural code search
      Automated code rewriting
      Custom lint rules
    Tech stack
      Rust
      tree-sitter
    Use cases
      Refactor code across a codebase
      Enforce team coding standards
      Write custom lint rules in YAML
    Key idea
      Patterns look like real code
      Wildcard matches any AST node
    Audience
      Developers
      Security researchers

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

What do people build with it?

USE CASE 1

Search a codebase for a specific code pattern regardless of variable names.

USE CASE 2

Rewrite matching code automatically across many files at once.

USE CASE 3

Write custom lint rules in YAML to enforce team coding standards.

USE CASE 4

Help a library's users migrate to a new API by scripting the rewrite.

What is it built with?

Rusttree-sitterCLI

How does it compare?

adsharma/ast-grepcodeitlikemiley/antigravity-sdk-rustcodeitlikemiley/duplex-server-rs
Stars333
LanguageRustRustRust
Last pushed2025-09-18
MaintenanceQuiet
Setup difficultyeasyhardmoderate
Complexity3/54/53/5
Audiencedeveloperdeveloperdeveloper

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · easy Time to first run · 5min

Installable via npm, pip, cargo, or homebrew with no extra configuration.

The README does not state a license.

So what is it?

ast-grep, often shortened to sg, is a command line tool for finding and rewriting code based on its structure rather than its exact text. Most developers are familiar with grep, which searches files for matching lines of text. ast-grep works the same way in spirit, but instead of matching plain text it understands the actual structure of the code, called an abstract syntax tree, so it can find code that has the same shape even if variable names or formatting differ. Patterns are written to look like ordinary code you would write anyway. A special wildcard symbol, a dollar sign followed by capital letters, can stand in for any piece of code, similar to how a dot works in a regular expression but applied to code structure instead of raw text. This makes it possible to search for a pattern like a certain function call and automatically rewrite every match into a new form, across an entire codebase at once. The tool is written in Rust and built on tree-sitter, a widely used code parsing library, which lets it work across many programming languages and take advantage of multiple processor cores for speed. It can be installed through common package managers such as npm, pip, cargo, or homebrew, or built directly from source. Beyond one-off search and replace commands, ast-grep also supports writing linting rules in YAML, so teams can define and enforce their own coding standards. The project describes several groups of people it aims to help: open source library maintainers who want to help their users adopt breaking changes more smoothly, tech leads who want to enforce coding practices tailored to their team, and security researchers who want to write detection rules quickly. An online playground is also available for trying out patterns without installing anything.

Copy-paste prompts

Prompt 1
Show me how to install ast-grep and run my first structural search.
Prompt 2
Help me write an ast-grep pattern to find and rewrite a specific function call in TypeScript.
Prompt 3
Explain how the $MATCH wildcard works in ast-grep patterns.
Prompt 4
Walk me through writing a custom lint rule for ast-grep using YAML.

Frequently asked questions

What is ast-grep?

A command line tool that searches and rewrites code by its structure instead of plain text, using patterns that look like ordinary code.

What language is ast-grep written in?

Mainly Rust. The stack also includes Rust, tree-sitter, CLI.

What license does ast-grep use?

The README does not state a license.

How hard is ast-grep to set up?

Setup difficulty is rated easy, with roughly 5min to a first successful run.

Who is ast-grep for?

Mainly developer.

Open on GitHub → Ask about another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.