Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/tree-sitter/tree-sitter/llms.txt

Use this file to discover all available pages before exploring further.

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.
tree-sitter query [OPTIONS] <QUERY_PATH> [PATHS]...
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

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

Query Range Options

--byte-range
start:end
The range of byte offsets in which the query will be executed. Format: start_byte:end_byte.
--containing-byte-range
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.
--row-range
start:end
The range of rows in which the query will be executed. Format: start_row:end_row.
--containing-row-range
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

-c, --captures
flag
Order the query results by captures instead of matches.
-q, --quiet
flag
Suppress main output.
-t, --time
flag
Print the time taken to execute the query on the file.

Test Options

--test
flag
Whether to run query tests or not.

Build Options

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

Examples

Query a Single File

tree-sitter query queries/highlights.scm example.js

Query Multiple Files

tree-sitter query queries/tags.scm src/**/*.js

Query with Byte Range

tree-sitter query queries/highlights.scm example.js --byte-range 100:500

Query with Row Range

tree-sitter query queries/highlights.scm example.js --row-range 10:20

Show Captures Instead of Matches

tree-sitter query queries/highlights.scm example.js --captures

Query and Measure Performance

tree-sitter query queries/highlights.scm example.js --time

Query from File List

tree-sitter query queries/highlights.scm --paths file-list.txt

Query Syntax

Queries use a Lisp-like syntax to match patterns in syntax trees. Example:
(function_declaration
  name: (identifier) @function.name
  parameters: (formal_parameters) @function.parameters)
For more information, see Pattern Matching with Queries.