Skip to main content
The query command runs a query on a parser and views the results. It allows you to search through source code using Tree-sitter query syntax.
Aliases: q

Query Path

The required QUERY_PATH argument specifies the path to a file containing Tree-sitter query patterns.

Source Paths

Optional PATHS arguments specify the source files to query. If no paths are provided, input is read from stdin.

Options

Input Options

file
The path to a file that contains paths to source files in which the query will be executed.
path
The path to the directory containing the grammar. Implies --rebuild.
path
The path to the parser’s dynamic library. Used instead of the cached or automatically generated library.
string
If --lib-path is used, the name of the language used to extract the library’s language function.
scope
The language scope to use for parsing and querying. Useful when the language is ambiguous.
number
Query the contents of a specific test.

Query Range Options

start:end
The range of byte offsets in which the query will be executed. Format: start_byte:end_byte.
start:end
The range of byte offsets in which the query will be executed. Only matches fully contained within the provided byte range will be returned.
start:end
The range of rows in which the query will be executed. Format: start_row:end_row.
start:end
The range of rows in which the query will be executed. Only matches fully contained within the provided row range will be returned.

Output Options

flag
Order the query results by captures instead of matches.
flag
Suppress main output.
flag
Print the time taken to execute the query on the file.

Test Options

flag
Whether to run query tests or not.

Build Options

flag
Force a rebuild of the parser before executing the query.
path
The path to an alternative configuration (config.json) file. See init-config.

Examples

Query a Single File

Query Multiple Files

Query with Byte Range

Query with Row Range

Show Captures Instead of Matches

Query and Measure Performance

Query from File List

Query Syntax

Queries use a Lisp-like syntax to match patterns in syntax trees. Example:
For more information, see Pattern Matching with Queries.