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

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.0
File Size Uploaded
ast_grep_cli-0.51.0.tar.gz 292.5 kB Details

Built distributions (wheels)

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

Total release size: 93.8 MB

Release files / ast_grep_cli-0.51.0.tar.gz

Download URL ast_grep_cli-0.51.0.tar.gz
Size 292.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3c6b0b4fe358dab092f56e196974cf77bd19465162b8260c78b837f6d2e2ad2c
BLAKE2b-256 checksum
How to use checksums
fff035a93aed849db95dd85e93d06bb6f97fb6856b8828c56920b68c3a325109
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","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.0-py3-none-win_arm64.whl

Download URL ast_grep_cli-0.51.0-py3-none-win_arm64.whl
Size 16.7 MB
Tags Python 3 Windows ARM64
SHA-256 checksum
How to use checksums
0c2f59d7881d8b4584d319865e7be4e97fa1a26ec09c02b7d2095b30af9a56e4
BLAKE2b-256 checksum
How to use checksums
7f226ecdc66e8251f65bfc2a942ae71267d899766c8cbf07c147dc20cf369cd4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","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.0-py3-none-win_amd64.whl

Download URL ast_grep_cli-0.51.0-py3-none-win_amd64.whl
Size 17.5 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
be2586df4192adb60fa808bd83c4f3a46c63315f48394400f07700f9e76f4dc6
BLAKE2b-256 checksum
How to use checksums
1e305a2f0c8f11c456c93324884f4d037ade45e0db09919942e19a098f1994e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","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.0-py3-none-win32.whl

Download URL ast_grep_cli-0.51.0-py3-none-win32.whl
Size 16.4 MB
Tags Python 3 Windows x86-32
SHA-256 checksum
How to use checksums
575f8d1a457e5dd1336a7c8db07cf4ae78cc3b8083f9529261016a2dd0510c9a
BLAKE2b-256 checksum
How to use checksums
064a9e0e50f1296c7968854d627cb5c549ce428cc30679b70083048b7c8a023d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","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.0-py3-none-manylinux_2_28_x86_64.whl

Download URL ast_grep_cli-0.51.0-py3-none-manylinux_2_28_x86_64.whl
Size 8.8 MB
Tags Linux glibc 2.28+ x86-64 Python 3
SHA-256 checksum
How to use checksums
17007574681fe872473850780304b3df57ca98ac575d88957972fea8c3810c26
BLAKE2b-256 checksum
How to use checksums
b5585f644df6460b0788871f9354b5a61797d0ac3adbeed2786fc872d79ced30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","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.0-py3-none-manylinux_2_28_aarch64.whl

Download URL ast_grep_cli-0.51.0-py3-none-manylinux_2_28_aarch64.whl
Size 8.4 MB
Tags Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
a134c0795d7e74b7d8530db96b06d82377784f4a30d488497c4fb1414e100c9f
BLAKE2b-256 checksum
How to use checksums
690c8b09bc9bb0e8f0698e6f3022157bb88cce23bd10937551c4b4e8ebffcf47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","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.0-py3-none-macosx_10_12_x86_64.whl

Download URL ast_grep_cli-0.51.0-py3-none-macosx_10_12_x86_64.whl
Size 8.6 MB
Tags Python 3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
ff724d3f03cf44b4f3567518acc325dcff3bd383b77778dc0753b66ce2080250
BLAKE2b-256 checksum
How to use checksums
e0d36b3c252a0973af188cb2800786087f20734d5779df20c0da754f106215b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","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.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl

Download URL ast_grep_cli-0.51.0-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Size 17.1 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
17f288ed74bf4e5cc8bfa7fda0e7d23f658cef2e7257216720df0ccb958be9d1
BLAKE2b-256 checksum
How to use checksums
e6034db443e62eb26a638035ba94a2017e98fd5b7377cfd53cfe19b711ba11b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.51.0 This release

8 release files

0.45.3

8 release files

0.45.2

8 release files

0.45.0

8 release files

0.44.0

8 release files

0.42.3

8 release files

0.42.2

8 release files

0.42.0

8 release files

0.41.1

8 release files

0.41.0

8 release files

0.40.5

8 release files

0.40.3

8 release files

0.40.1

8 release files

0.40.0

8 release files

0.39.7

8 release files

0.39.4

8 release files

0.39.2

8 release files

0.39.1

8 release files

0.39.0

8 release files

0.38.7

8 release files

0.38.3

8 release files

0.38.2

8 release files

0.38.1

8 release files

0.38.0

8 release files

0.37.0

8 release files

0.36.3

8 release files

0.36.2

8 release files

0.36.1

8 release files

0.35.0

8 release files

0.34.3

8 release files

0.34.2

8 release files

0.34.1

8 release files

0.34.0

8 release files

0.33.1

8 release files

0.32.3

8 release files

0.32.2

8 release files

0.32.1

8 release files

0.32.0

8 release files

0.30.1

8 release files

0.30.0

8 release files

0.29.0

8 release files

0.28.1

8 release files

0.27.3

8 release files

0.27.2

8 release files

0.26.3

8 release files

0.26.2

8 release files

0.25.4

8 release files

0.25.3

8 release files

0.25.2

8 release files

0.25.1

8 release files

0.25.0

8 release files

0.24.1

8 release files

0.24.0

8 release files

0.23.1

8 release files

0.23.0

8 release files

0.22.4

8 release files

0.22.3

8 release files

0.22.2

8 release files

0.22.1

8 release files

0.21.1

8 release files

0.20.5

8 release files

0.20.2

8 release files

0.20.1

8 release files

0.20.0

8 release files

0.19.4

8 release files

0.19.3

8 release files

0.19.2

8 release files

0.19.1

8 release files

0.19.0

8 release files

0.18.0

8 release files

0.17.1

8 release files

0.17.0

8 release files

0.16.0

8 release files

0.15.1

8 release files

0.15.0

8 release files

0.14.4

8 release files

0.14.3

8 release files

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