Skip to main content

ast-grep

coverage badge Discord Repology Badge Badge GitHub Sponsors Gurubase

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!

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
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

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

ast-grep -p '$A && $A()' -l ts -r '$A?.()'
ast-grep -p 'new Zodios($URL,  $CONF as const,)' -l ts -r 'new Zodios($URL, $CONF)' -i

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, ast-grep can help you write rules much faster.

Metadata

Release files for ast-grep-cli 0.51.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ast-grep-cli 0.51.2
File Size Uploaded Provenance Actions
ast_grep_cli-0.51.2.tar.gz 299.9 kB Without provenance Details for ast_grep_cli-0.51.2.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for ast-grep-cli 0.51.2
File Actions
ast_grep_cli-0.51.2-py3-none-win_arm64.whl Python 3 none Windows ARM64 Details for ast_grep_cli-0.51.2-py3-none-win_arm64.whl
ast_grep_cli-0.51.2-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for ast_grep_cli-0.51.2-py3-none-win_amd64.whl
ast_grep_cli-0.51.2-py3-none-win32.whl Python 3 none Windows x86-32 Details for ast_grep_cli-0.51.2-py3-none-win32.whl
ast_grep_cli-0.51.2-py3-none-manylinux_2_28_x86_64.whl Python 3 none Linux glibc 2.28+ x86-64 Details for ast_grep_cli-0.51.2-py3-none-manylinux_2_28_x86_64.whl
ast_grep_cli-0.51.2-py3-none-manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.28+ ARM64 Details for ast_grep_cli-0.51.2-py3-none-manylinux_2_28_aarch64.whl
ast_grep_cli-0.51.2-py3-none-macosx_10_12_x86_64.whl Python 3 none macOS 10.12+ x86-64 Details for ast_grep_cli-0.51.2-py3-none-macosx_10_12_x86_64.whl
ast_grep_cli-0.51.2-py3-none-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64, macOS 10.12+ universal2 (ARM64, x86-64), macOS 10.12+ x86-64 Details for ast_grep_cli-0.51.2-py3-none-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl

Total release size: 95.3 MB

Release files / ast_grep_cli-0.51.2.tar.gz

Download URL ast_grep_cli-0.51.2.tar.gz
Size 299.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7e787c6fe171026e16fb9761a6d3b50cbc6f519b893e7cff2e66dfde95145c81
BLAKE2b-256 checksum
How to use checksums
6f0c6197f089e762c7c82fe23b895dedd515cd2ed16d477f9983b5b3e50d4586
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ast_grep_cli-0.51.2-py3-none-win_arm64.whl

Download URL ast_grep_cli-0.51.2-py3-none-win_arm64.whl
Size 17.0 MB
Tags Python 3 Windows ARM64
SHA-256 checksum
How to use checksums
ba0f2d5d672bdd2d6926ce7fc7c062b15d468af73c2d35a1244fc853f4cf93f4
BLAKE2b-256 checksum
How to use checksums
a40121ff76e85c8fe1ec6a182ed2296f28a859c0ca85c05d950e4eb2059e3ee6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ast_grep_cli-0.51.2-py3-none-win_amd64.whl

Download URL ast_grep_cli-0.51.2-py3-none-win_amd64.whl
Size 17.8 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
b8aad3396a78e1129c2e00f664f8c6965d9c1c1329140722d607517f69ad279a
BLAKE2b-256 checksum
How to use checksums
b72b715cd3706e160f624c8da574d8acdf78249a9858d9331fa2cbfe19f7a5ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ast_grep_cli-0.51.2-py3-none-win32.whl

Download URL ast_grep_cli-0.51.2-py3-none-win32.whl
Size 16.7 MB
Tags Python 3 Windows x86-32
SHA-256 checksum
How to use checksums
a20d49bf6f3305c026f93e964c602819712af9caec8df44d278c7940707685f9
BLAKE2b-256 checksum
How to use checksums
22b8ac07fd5478d3a43e2d7b560bce6060df1071935fe210425637985d171896
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ast_grep_cli-0.51.2-py3-none-manylinux_2_28_x86_64.whl

Download URL ast_grep_cli-0.51.2-py3-none-manylinux_2_28_x86_64.whl
Size 8.9 MB
Tags Linux glibc 2.28+ x86-64 Python 3
SHA-256 checksum
How to use checksums
461f5ea36dbc0d8e3b965128deb0ac81b17f41b74b28da909cf836ce95953027
BLAKE2b-256 checksum
How to use checksums
c6e30531ce742e1c8df48dc51b7eaf6fcf0c10aa3d52e51ba82a720658248d99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ast_grep_cli-0.51.2-py3-none-manylinux_2_28_aarch64.whl

Download URL ast_grep_cli-0.51.2-py3-none-manylinux_2_28_aarch64.whl
Size 8.5 MB
Tags Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
571f8c7cb2c7de16c52cf7862f2a528c67eb87d084e335a3f7366c7fa745158c
BLAKE2b-256 checksum
How to use checksums
cb53825dbe31554952870173af79eb032b079da36a2387f284e99c97f1bdcd78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ast_grep_cli-0.51.2-py3-none-macosx_10_12_x86_64.whl

Download URL ast_grep_cli-0.51.2-py3-none-macosx_10_12_x86_64.whl
Size 8.7 MB
Tags Python 3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
6a2f528fc10d668b220b512e7e34466a13b4ba7b5ee95d1596a2b1e194a0a6cf
BLAKE2b-256 checksum
How to use checksums
b6ef1cd2da2353ff8e6db008d8a6d372dc413c1d09127c82525323bdd74eb747
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ast_grep_cli-0.51.2-py3-none-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl

Download URL ast_grep_cli-0.51.2-py3-none-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl
Size 17.4 MB
Tags Python 3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4301bfa472b7fa77dadb639173e3908bd0b97ffe3e164ab62e03c14e94669616
BLAKE2b-256 checksum
How to use checksums
6e086e7843c7061b92fed10ecc604bf9263e65050325b1a5e73ee7c9d6d8b005
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for ast-grep-cli 0.51.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.51.2 This release

8 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page