Skip to content

ast-grep

A command-line tool that searches, lints, and rewrites code by its syntax structure, not plain text.

Stars
16.4k
Forks
488
Licence
MIT
Language
Rust
Last update
4 days ago
Type
CLI

What it is

ast-grep is a command-line tool that searches code by matching its syntax tree rather than raw text, so a pattern written like ordinary code can find every place that shares the same structure. It can also rewrite matches and run YAML-defined lint rules. People use it to find usages, migrate code across breaking changes, or enforce team coding standards.

  • Find all calls matching a code pattern across a project
  • Rewrite a deprecated API call to its replacement across many files
  • Write a YAML lint rule that flags a specific code pattern
  • Try patterns interactively in the online playground before running them

Great for

  • Developers who want grep-like searching that understands code structure
  • Library authors migrating users across breaking API changes
  • Tech leads who want to enforce custom code practices with lint rules
  • Engineers doing large-scale, consistent code rewrites

Not ideal for

  • People who only need simple text search, since plain grep or ripgrep is simpler for that
  • Non-programmers, because patterns assume familiarity with code syntax
  • Languages not supported by its tree-sitter parsers, which the README does not fully list
Step-by-step guide: how to use ast-grepInstall, first run, examples, tips and FAQs

How to get it

  1. 1

    Install with a package manager

    Install via npm

    npm install --global @ast-grep/cli

    Install via pip

    pip install ast-grep-cli

    Install via Homebrew

    brew install ast-grep

    Install via cargo

    cargo install ast-grep --locked
  2. 2

    Run it

    • Install ast-grep using one of the package manager commands above.
    • Run a search with: ast-grep --pattern 'var code = $PATTERN' --lang ts
    • Add --rewrite to replace matches with a new pattern.
    • Try example patterns in the online playground at ast-grep.github.io/playground.html.
  3. 3

    Get the most out of it

    • Use $ wildcards such as $MATCH to match any single syntax node.
    • Write YAML configuration files to define custom lint rules.
    • Use the jQuery-like API for traversing and manipulating the syntax tree.
    • Run it across multiple cores, as the README notes it uses multiple cores for parsing.
    • Check the official website for more screenshots and documentation.

Downloads

Strengths

  • Patterns look like the code you already write
  • Matches by syntax tree, so it ignores formatting differences
  • Installable through many package managers including npm, pip, Homebrew, and cargo

Trade-offs

  • Its matching depends on the language grammars it supports, so coverage varies by language
  • The README is truncated and does not describe every feature or limitation in detail

How it compares

ProjectStarsTypeLevelUpdatedBest for
ast-grep (this)16.4kCLIbeginner4 days agoDevelopers who want grep-like searching that understands code structure
jscodeshift10kDev toolintermediate9 days agoEngineers migrating a large JavaScript or TypeScript codebase to a new API or syntax
ruff50.4kCLIbeginner4 days agoPython developers who want one tool for linting and formatting
broot13.1kCLIintermediate5 days agoDevelopers who work in a terminal and often jump between project folders
babel44.1kDev toolintermediate4 days agoWeb developers who want to use recent JavaScript syntax while still supporting older browsers
code-review-graph32.6kDev toolbeginner8 days agoDevelopers who use an MCP-compatible AI coding assistant on a large repository

Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.