← 返回命令列表

Linux command

ast-grep 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Search for a pattern

ast-grep --pattern 'console.log($ARG)'

Search with a specific language

ast-grep --pattern 'async function $NAME() {}' --lang typescript

Search and rewrite

ast-grep --pattern '$OLD.forEach($FN)' --rewrite '$OLD.map($FN)'

Interactive rewrite

ast-grep --pattern '$A == $B' --rewrite '$A === $B' --interactive

Output matches as JSON

ast-grep --pattern 'var $X = $Y' --json

Scan using rule configuration

ast-grep scan

Search with context lines

ast-grep run --pattern '$FUNC()' --context 3

Filter files with a glob

ast-grep run --pattern '$X' --globs '*.ts'

说明

ast-grep (also invoked as sg) is a structural code search and rewriting tool. Unlike text-based grep, it parses code into an Abstract Syntax Tree (AST) and matches patterns at the syntactic level, ensuring matches respect code structure. Patterns use a code-like syntax where $METAVARIABLES match any expression. For example, console.log($MSG) matches any console.log call regardless of its argument. This enables precise refactoring that text-based tools cannot achieve. The tool supports multiple languages through tree-sitter parsers, including JavaScript, TypeScript, Python, Rust, Go, C, C++, Java, and more. The scan command applies rules from YAML configuration files, enabling project-wide linting and enforcement of code patterns. Configuration files (sgconfig.yml) define rule directories and project settings. Individual rules specify patterns, rewrites, severity levels, and file filters.

参数

run
Run a one-time search or rewrite (default command).
scan
Scan and rewrite code using YAML configuration rules.
test
Test ast-grep rules against test cases.
new
Create new ast-grep project, rules, or tests via scaffolding.
lsp
Start language server for IDE integration.
-p _pattern_, --pattern _pattern_
AST pattern to search for. Use $VAR for metavariables.
-r _replacement_, --rewrite _replacement_
Replacement pattern for matches. Use $VAR to reference captured metavariables.
-l _lang_, --lang _lang_
Target language (javascript, typescript, python, rust, go, c, cpp, java, etc.).
-i, --interactive
Interactive mode for confirming rewrites one by one.
--json _style_
Output in JSON format (pretty, stream, compact).
-A _num_, --after _num_
Show num lines after each match.
-B _num_, --before _num_
Show num lines before each match.
-C _num_, --context _num_
Show num lines around each match.
--globs _pattern_
Include or exclude files matching the given glob. May be specified multiple times.
--stdin
Read code from standard input.
--threads _num_
Number of threads (0 for auto-detection).
-c _file_, --config _file_
Path to config file (default: sgconfig.yml).

FAQ

What is the ast-grep command used for?

ast-grep (also invoked as sg) is a structural code search and rewriting tool. Unlike text-based grep, it parses code into an Abstract Syntax Tree (AST) and matches patterns at the syntactic level, ensuring matches respect code structure. Patterns use a code-like syntax where $METAVARIABLES match any expression. For example, console.log($MSG) matches any console.log call regardless of its argument. This enables precise refactoring that text-based tools cannot achieve. The tool supports multiple languages through tree-sitter parsers, including JavaScript, TypeScript, Python, Rust, Go, C, C++, Java, and more. The scan command applies rules from YAML configuration files, enabling project-wide linting and enforcement of code patterns. Configuration files (sgconfig.yml) define rule directories and project settings. Individual rules specify patterns, rewrites, severity levels, and file filters.

How do I run a basic ast-grep example?

Run `ast-grep --pattern 'console.log($ARG)'` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does run do in ast-grep?

Run a one-time search or rewrite (default command).