<!-- generated by build_articles.py — edit content/answers/syntax-aware-code-search-refactoring.md instead -->
---
title: "Syntax-aware code search: XERJ vs ast-grep"
canonical: "https://xerj.org/answers/syntax-aware-code-search-refactoring"
updated: "2026-11-13"
source: "content/answers/syntax-aware-code-search-refactoring.md"
---

# Syntax-aware code search: XERJ vs ast-grep

**Short answer** (updated 2026-11-13): How do I run syntax-aware code search, and how does it compare to ast-grep? XERJ uses tree-sitter to store each declared symbol as kind, name and line, across 36 languages. Run xerj search --field defs or xerj def to find declarations. XERJ rewrites nothing. For a structural rewrite, use ast-grep or a language server.

**TL;DR** Syntax-aware code search in XERJ is a tree-sitter pass during autoindex: each declared symbol is stored as kind, name and line, across 36 languages. Run `xerj search --field defs` or `xerj def` to find declarations, and a body query to find the text. XERJ rewrites nothing. For a structural rewrite, use ast-grep or a language server.

## Agent prompt

```text
Read https://xerj.org/llms.txt. Install xerj, autoindex the repo, then run xerj search --field defs for declarations or xerj def for a named symbol. Prefer a body query when you want call sites, not only declarations. Do not use xerj to rewrite source; use ast-grep or a language server for a structural rewrite. Quote file:line, and say so when a query returns no hits.
```

## Commands

### Command 1

Note: Install the release binary into ~/.local/bin.

```sh
curl -fsSL https://xerj.org/get | sh
```

### Command 2

Note: Index the tree. Code files are parsed with tree-sitter during this pass.

```sh
xerj autoindex .
```

### Command 3

Note: Files that declare the name, not every mention in the body.

```sh
xerj search merge_segments --field defs
```

### Command 4

Note: Raw text in the file body, including call sites.

```sh
xerj search merge_segments --field body
```

### Command 5

Note: Named-symbol lookup: kind, file, line, and a short excerpt.

```sh
xerj def merge_segments --json
```

The concept is [What is syntax-aware search?](/science/syntax-aware-search).

*Figure: XERJ behaviour follows engine/crates/xerj-autoindex/src/extract/code.rs (36-language registry, 2 MiB code cap, 5,000 symbols per file, 200-byte name cap). The merge_segments and MergeSegments results are from the three-file capture described on this page (rust, python, go). No large repository was measured here.*

## What each tool is for

XERJ and ast-grep both read syntax, and they do different jobs.

XERJ is a search index. During `xerj autoindex`, it parses each source file with tree-sitter and writes declared symbols into a `defs` field and a `symbols` array (kind, name, line). You query that index later: a defs query returns the files that declare a name; a body query returns raw text, including call sites. The result is file:line, for a coding agent to read.

ast-grep is a structural search and rewrite tool. It matches a pattern against a syntax tree and can apply an edit. Use it when the job is to change the code, or when a rename must be sound. This page does not report an ast-grep benchmark. The split is about the job, not a speed claim.

If you already know the exact string and want every line, [ripgrep](/compare/xerj-vs-ripgrep-for-code-agents) is still the right tool. The [local MCP answer](/answers/local-code-search-mcp-fewer-tokens-than-grep) covers ranked passages versus grep in tokens and bytes.

*Figure: Both queries from the capture on this page, run against one index on 1 single-node XERJ process. Scores are the _score values returned.*

## Run the two queries

Index the tree once, then search. This example uses a name from the capture on this page.

```sh
curl -fsSL https://xerj.org/get | sh
xerj autoindex .
xerj search merge_segments --field defs
xerj search merge_segments --field body
xerj def merge_segments --json
```

A defs query answers "where is this declared?". A body query answers "where does this text appear?". `xerj def` returns the declaration: kind, file, line and a short excerpt, which is the form a coding agent can quote.

*Figure: The 4 entries in the symbols array of src/lib.rs , each with a kind, a name and the line of its declaration. The axis is drawn to line 20. A rename tool starts from these lines.*

## What the capture showed

The capture used one node and three files, in rust, python and go. Both the body query and the defs query returned 2 hits on that tree. That is a small tree, labelled as a capture, not a repository-scale measurement.

Case matters. `src/query.go` declares `MergeSegments`, and the token `merge_segments` did not match it. If a grammar fails to parse a file, XERJ indexes that file as plain text, so a body query still works. Extraction is definitions only: there is no call graph, and there is no cross-file reference resolution.

The 36-language list lives in `engine/crates/xerj-autoindex/src/extract/code.rs` and is pinned by a unit test.

*Figure: Caps from extract/code.rs . A file that a grammar fails to parse is indexed as plain text, not dropped.*

## Caps, and when not to use this

A code file larger than 2 MiB is skipped (`CODE_CAP`). At most 5,000 symbols are stored per file. A symbol name longer than 200 bytes is dropped. Those caps are in the same extract module.

Do not use XERJ to rewrite source. It has no rewrite command. For a structural rewrite, use ast-grep or a language server.

Do not treat a defs query as a reference or call-site search. It finds declarations only.

Do not expect a case-insensitive match. The capture missed `MergeSegments` when the query was `merge_segments`.

Wiring these queries into Claude Code over MCP is in [code search MCP for Claude Code](/answers/code-search-mcp-for-claude-code). For the coding-agent loop that retrieves a declaration before it writes, see the [reference-coding case study](/case-studies/reference-coding).

Use the Copy the prompt button if you want an agent to run a defs or body query. Ask it to quote file:line, and to leave rewrites to ast-grep or a language server.

## FAQ

### How do I run syntax-aware code search in XERJ?

Install xerj and run xerj autoindex on the repository. Then run xerj search --field defs to find declarations, or xerj def for a named symbol. A body query finds the raw text, including call sites.

### How does XERJ compare to ast-grep?

XERJ finds declarations and text, with file:line. It rewrites nothing. ast-grep matches syntax trees and can apply a structural rewrite. Use XERJ to locate a symbol; use ast-grep when the job is to change the code.

### How many languages does the tree-sitter pass cover?

36, in the registry in engine/crates/xerj-autoindex/src/extract/code.rs, pinned by a unit test. If a grammar fails to parse a file, XERJ indexes that file as plain text, so a body query still works.

### Does a defs query find every mention of a name?

No. It finds files that declare the name. Use a body query for call sites. There is no call graph and no cross-file reference resolution.

### Are the queries case-sensitive?

Yes, in the capture on this page. src/query.go declares MergeSegments, and the token merge_segments did not match it.

### What are the caps?

A code file larger than 2 MiB is skipped. At most 5,000 symbols are stored per file. A symbol name longer than 200 bytes is dropped.

## Evidence

- 36 languages in the tree-sitter registry, pinned by a unit test; CODE_CAP 2 MiB; max 5,000 symbols per file; name cap 200 bytes; definitions only, no call graph Source: `engine/crates/xerj-autoindex/src/extract/code.rs`

## Related

- [What is syntax-aware search?](/science/syntax-aware-search)
- [Local code search MCP that uses fewer tokens than grep](/answers/local-code-search-mcp-fewer-tokens-than-grep)
- [I need my agent to find a passage and cite the file, not dump the whole tree. How?](/answers/code-search-mcp-for-claude-code)
- [Chat with a GitHub repo without cloning it](/answers/ask-questions-about-a-github-repo-without-cloning)
- [Is ripgrep enough for a coding agent, or do I need a code index?](/compare/xerj-vs-ripgrep-for-code-agents)
- [Reference-coding for Claude Code](/case-studies/reference-coding)
