grep-ast
A tool to grep through the AST of a source file
What it is and what it does
grep-ast is a command-line search tool that finds patterns in source code and displays matching lines along with their syntactic context—the enclosing functions, classes, loops, and other code blocks they belong to. It uses tree-sitter to parse code into an abstract syntax tree, then shows you not just the match but how it fits into the broader structure of the file. This is useful when you need to understand not just where a pattern appears, but what code context surrounds it.
The tool respects .gitignore by default and recurses through directories, so you can run it from a repository root without specifying files. It supports many popular programming languages via tree-sitter-language-pack and can be invoked as either `grep-ast` or `gast`. It offers basic options like case-insensitive matching, color control, and encoding specification.
Use it for:
- Find all uses of a function name and see which functions or classes call it and in what context
- Search for a variable name and understand the scope and surrounding logic where it appears
- Locate a specific pattern across a codebase and see the class or method definitions that contain each match
- Explore a large unfamiliar codebase by searching for key terms and seeing their structural context
- Refactor code by finding all occurrences of a pattern and understanding their relationships to surrounding code
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Search source code files by pattern and display matching lines with their surrounding context in the abstract syntax tree, showing how matches fit into functions, classes, loops, and other code structures.
Yes, if you work with source code search and want to understand context. Low install friction and permissive license make it a low-risk addition. Aging maintenance (463 days since last release) is a minor concern but the repository is active and has reasonable adoption. No known vulnerabilities. Install if you need AST-aware code search; skip if you only need simple text grep.
Install
grep-ast on PyPI
pip
pip install grep-astuv
uv add grep-astpoetry
poetry add grep-astInstalling grep-ast
Before you install
Low install friction; pure Python wheel. Maintenance status is aging—last release was 463 days ago—but the repository is active and not archived, with 362 stars.
License in practice
Licensed under Apache Software License (permissive), so you can use, modify, and distribute the package freely with minimal restrictions.
Quickstart
pip install grep-ast
grep-ast 'pattern' /path/to/code
# or with options:
grep-ast -i --color 'pattern' src/
Verify before relying
- Whether Python version support is truly unspecified or if there is a practical minimum version requirement
- How well the tool handles very large codebases or deeply nested syntax trees
- Whether the .gitignore-respecting behavior works correctly across all supported operating systems
Package facts
| License | not declared (permissive) |
| Python support | not specified |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — tree-sitter-language-pack, pathspec |
| Maintenance | aging — 463 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,768,321/month — #3,574 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: grep_ast-0.9.0-py3-none-any.whl
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
ast-grep-cliast-grep-cli is a command-line tool for…
permissive · top 5,000 on PyPI
tensor-grepA high-performance grep-compatible command-line…
unclear · top 15,000 on PyPI
gronTransforms JSON into grep-able line-by-line…
permissive · top 15,000 on PyPI
tree-sitter-zigProvides a tree-sitter grammar for parsing Zig…
permissive · top 5,000 on PyPI
trailmarkParses source code into a queryable graph of…
permissive · top 15,000 on PyPI
ast-grep-pyast-grep-py is a Python binding for ast-grep, a…
permissive · top 5,000 on PyPI
ripgrepProvides a Python interface to ripgrep (rg), a…
permissive · top 15,000 on PyPI
tree-sitter-powershellProvides a tree-sitter grammar for parsing…
permissive · top 5,000 on PyPI
tree-sitter-juliaProvides incremental parsing of Julia source…
permissive · top 5,000 on PyPI
tree-sitter-groovyProvides a Groovy language grammar for…
permissive · top 5,000 on PyPI