# ast-grep

> A CLI tool for code structural search, lint and rewriting. Written in Rust

- Type: agent or tool
- Field: Code Review & Testing
- Repository: https://github.com/ast-grep/ast-grep
- GitHub stars: 16688
- Licence: MIT
- Language: Rust
- Last push: 2026-11-24
- Page: https://imutechz.in/agents/ast-grep

## What it is
ast-grep (sg) is a command-line tool for structural search, lint and rewriting of code. Instead of matching text like grep, it matches abstract syntax tree nodes produced by tree-sitter, so patterns written as ordinary code match all code with the same syntactic structure. Wildcards such as $MATCH match any single AST node. It is aimed at developers, library authors, tech leads and security researchers doing static analysis or large-scale code changes.

## Features
- Pattern-based search that matches AST nodes rather than text
- Wildcards like $MATCH to match any single AST node
- Rewrite support to transform matched code
- YAML configuration for custom linting rules and code modification
- jQuery-like API for AST traversal and manipulation
- Written in Rust with tree-sitter parsing and multi-core use

## Best for
- Finding code by structure across a codebase
- Large-scale code rewriting and migrations
- Writing custom lint rules in YAML
- Lightweight static analysis

## Pros
- Patterns look like ordinary code, so they are intuitive to write
- Supports search, lint and rewriting in one CLI
- Rules can be defined in YAML
- Available through many package managers

## Cons
- Installing from npm requires Node.js 20 or later
- Building from source requires rustup and cloning the repository

## Install
```
npm install --global @ast-grep/cli
```

## Usage
```
ast-grep -p '$A && $A()' -l ts -r '$A?.()'
```

Tags: ast-grep, structural-search, code-search, lint, code-rewriting, tree-sitter, cli, rust
