grep-ast is a command-line tool designed to search through the Abstract Syntax Tree (AST) of source code files. Unlike traditional `grep` which operates on text, `grep-ast` provides a context-aware output, showing how matching lines fit into the broader code structure (functions, classes, etc.). It leverages `tree-sitter` and `tree-sitter-languages` for parsing various programming languages. The current version is 0.9.0, released in May 2025.
pip install grep-astNo compatibility data collected yet for this library.
To quickly search for a pattern in a specific file or recursively in the current directory, use the `grep-ast` command followed by your pattern and optional filenames or paths. By default, it respects .gitignore. You can also use `gast` as an alias.
Ensure you are using the correct tool for your desired functionality; if you need AST-pattern matching or rewriting, consider `ast-grep` or `ast-grep-py`.
Formulate your patterns as standard regular expressions. If you require matching against the structural properties of the AST directly, tools like `ast-grep` are designed for that.
Explicitly specify target files or directories to limit the search scope. Use the `--no-gitignore` option if you need to bypass `.gitignore` filtering (check the latest CLI options for availability).
Ensure `grep-ast` is installed correctly and its installation directory (often within the Python environment's `bin` or `Scripts` folder) is in your system's PATH. Alternatively, run it directly using `python -m grep_ast [pattern] [filenames...]`.
Install the package using pip: `pip install grep-ast`. If you intend to install from the source, use `python -m pip install git+https://github.com/paul-gauthier/grep-ast.git`. Ensure you are working in the correct Python environment.
Simplify your pattern and use the `--verbose` flag (or a similar debug option if available, like `ast-grep`'s `--debug-query=ast`) to inspect the actual AST structure of the code you're trying to match, then adjust your pattern to reflect the AST nodes and their relationships.
Check the list of supported languages by consulting the `grep-ast` documentation or using `grep-ast --languages` if that option is available. Ensure the file extension is correctly recognized or explicitly specify the language using a `--lang` argument if the tool provides one.