ast-grep

Structural code search, lint and rewrite on the syntax tree, with patterns written like ordinary code

CLIMedium risk

ast-grep/ast-grep

Install

brew install ast-grep

Also: npm install --global @ast-grep/cli, pip install ast-grep-cli, cargo install ast-grep --locked.

This is third-party code. Review the repository files before installing.

What it does

ast-grep searches code by syntax rather than text: a pattern is written as a code fragment, and metavariables like $A match any tree node. Matches can be rewritten with a replacement pattern right away. Lint and codemod rules are defined in YAML. Parsing uses tree-sitter, and the tool is written in Rust and runs on multiple cores. For an agent this means precise project-wide refactors instead of fragile regular expressions.

Who it is for. For developers and agents who need syntax-aware code search and bulk edits.

Good fit when

  • You need to replace a deprecated API call across the project
  • You want a custom lint rule for the team
  • A regular expression over-matches or misses multi-line constructs

Not a fit when

  • You need to replace plain text or config without language syntax
  • You need type- and import-aware renames, where a language server fits better

Example request

Use ast-grep to replace every a && a() with a?.() in all TypeScript files

Limitations

Works only with languages that have a tree-sitter grammar. It has no type information, so constructs with the same shape but different meaning match alike. Patterns and YAML rules take some learning.

How to disable. Uninstall with the same package manager: npm uninstall -g @ast-grep/cli, pip uninstall ast-grep-cli or brew uninstall ast-grep.

Security check

  • Rewrites project files in bulk when replacing

README in short

The README explains syntax-tree search and metavariable syntax. It installs via npm, pip, cargo, Homebrew, scoop, MacPorts, Nix and mise. It shows TypeScript rewrite examples and lists key features: code-like patterns, a tree traversal API and YAML rules. MIT licensed.

FAQ

Why not grep or sed?

Matching follows code structure, so whitespace, line breaks and formatting do not matter, and strings or comments are not hit by accident.

Can I try it without installing?

Yes, the project website has an online playground.

Editors’ pick

A skills library that gives coding agents a development process: brainstorming, planning, TDD, subagents and code review

PluginMedium risk286.6K
Editors’ pick

Small composable skills for engineering with agents: plan grilling, TDD, bug diagnosis, code review and architecture

SkillLow risk262K

Reference MCP servers

Model Context Protocol servers

Editors’ pick

Official reference MCP servers: Filesystem, Fetch, Git, Memory, Sequential Thinking, Time and Everything

MCP serverMedium risk90.3K
Editors’ pick

Up-to-date, version-specific library docs and code examples in your agent's context, via MCP or a CLI plus skill

MCP serverMedium risk62K