ast-grep /ast-grep
⚡A CLI tool for code structural search, lint and rewriting. Written in Rust
★ 16,587⑂ 503👁 41⑃ 2,157● 775▤ 187
+7
Daily
16,587
stars
503
forks
41
watchers
2,157
prs
775
issues
187
releases
README
ast-grep(sg) ast-grep(sg) is a CLI tool for code structural search, lint, and rewriting. Introduction ast-grep is an abstract syntax tree based tool to search code by pattern code. Think of it as your old-friend grep, but matching AST nodes instead of text. You can write patterns as if you are writing ordinary code. It will match all code that has the same syntactical structure. You can use $ sign + upper case letters as a wildcard, e.g. $MATCH, to match any single AST node. Think of it as regular expression dot ., except it is not textual. Try the online playground for a taste! Screenshot !demo See more screenshots on the website. Installation You can install it from npm, pip, cargo, cargo-binstall, homebrew, scoop, mise or MacPorts! bash npm install --global @ast-grep/cli pnpm approve-builds may be needed pip install ast-grep-cli brew install ast-grep Click for more installation methods bash cargo install ast-grep --locked cargo binstall ast-grep install via scoop, thank @brian6932 scoop install main/ast-grep install via MacPorts sudo port install ast-grep try ast-grep in nix-shell nix-shell -p ast-grep try ast-grep with mise mise use -g ast-grep Or you can build ast-grep from source. You need to install rustup, clone the repository and then bash cargo install --path ./crates/cli --locked Packages are available on other platforms too. Command line usage example ast-grep has following form. ast-grep --pattern 'var code = $PATTERN' --rewrite 'let code = new $PATTERN' --lang ts Example Rewrite code in null coalescing operator bash ast-grep -p '$A && $A()' -l ts -r '$A?.()' Rewrite Zodios bash ast-grep -p 'new Zodios($URL, $CONF as const,)' -l ts -r 'new Zodios($URL, $CONF)' -i Implement eslint rule using YAML. Sponsor !Sponsors If you find ast-grep interesting and useful for your work, please buy me a coffee so I can spend more time on the project! Feature Highlight ast-grep's core is an algorithm to search and replace code based on abstract syntax tree produced by tree-sitter. It can help you to do lightweight static analysis and massive scale code manipulation in an intuitive way. Key highlights: An intuitive pattern to find and replace AST. ast-grep's pattern looks like ordinary code you would write every day (you could say the pattern is isomorphic to code). jQuery like API for AST traversal and manipulation. YAML configuration to write new linting rules or code modification. Written in compiled language, with tree-sitter based parsing and utilizing multiple cores. Beautiful command line interface :) ast-grep's vision is to democratize abstract syntax tree magic and to liberate one from cumbersome AST programming! If you are an open-source library author, ast-grep can help your library users adopt breaking changes more easily. if you are a tech lead in your team, ast-grep can help you enforce code best practice tailored to your business need. If you are a security researcher
Top issues
#156[refactor] use termcolor crate instead of ansi15 comments · 2023-01-03#56command `sg` is in the process of deprecation15 comments · 2022-09-23#373Who is using ast-grep?11 comments · 2023-05-14#2358Support Rule Paths from GitHub URLs10 comments · 2025-11-25#1963[feature] Plugin system9 comments · 2025-04-29#1141[feature] debugger REPL?9 comments · 2024-05-12#1790[feature] move ast-grep/napi to ESM6 comments · 2025-02-05#548[feature] Add pager for interactive search of multiple line match and context match5 comments · 2023-07-24
Discussions
#3066Using ast-grep to turn coding-agent instructions (CLAUDE.md / AGENTS.md) into lint rules0 comments · 2026-11-05#2991How to select statements from a single switch/case branch?4 comments · 2026-10-07#2898How to match full mod_name::mod_element in a Rust macro!()1 comments · 2026-08-20#2872let outline specified language only1 comments · 2026-08-06#2866Changed result matching comment in JS class member in ^0.45.02 comments · 2026-08-03#2826ast-grep outline: any way to make exactly? the same output when multiple execution on the same workspace?1 comments · 2026-07-20#2728Git history suggests ast-grep's core knowledge is still highly concentrated. Does that match reality?1 comments · 2026-06-17#2618How can I teach GitHub Copilot to use ast-grep?1 comments · 2026-05-04
Latest release
0.51.2 0.51.2 2026-11-11- chore(deps): update rust crate tree-sitter-scala to v0.26.10 3085 - feat(language): define feature-gated languages with macros 3081 - docs: fix dead wasm-pack installer link in the wasm crate README 3084 - chore(deps): update dependency oxlint to v1.93.0 3082 - fix(config): do not split words at digits in convert transform 3070 - chore(deps): update dependency web-tree-sitter to v0.27.2 3075 - chore(deps): update dependency node to v26 3069 - chore(deps): update rust crate toml_edit to v0.25.19 3071 - chore(deps): update rust crate tree-sitter-scala to v0.26.9 3072 - chore(deps): update depe