---
title: "Search the code"
description: "Search main's files with regular expressions, answered by the forge's own index, and only in the files you may read."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.forge.workreef.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Search the code

`af search` searches the files on main with a regular expression. The forge answers it from a trigram index it keeps of main, as Zoekt and Google's code search do, so a search does not depend on your workspace: it finds files your workspace does not hold. It searches only the files you may read.

## Find lines

Start a server and land some code:

```console server
$ af-server --dev
...
```

```console check
$ source ~/af-dev/dev/env
$ af workspace add ~/demo
Workspace at /home/you/demo
Paths: **
main has no landing yet
$ cd ~/demo
$ af new -m "Add a small program"
Working on change omukmnmmoypqyylrxspyusnmkpymtzyp at commit 45e3a1f056aa3879dceae71d2d5d7ce3f3220d46380cdc4285974d9e8a213e82
$ mkdir -p src docs
$ printf 'fn main() {\n    // TODO: greet by name\n    println!("Hello");\n}\n' > src/main.rs
$ printf '# Notes\n\nTODO: write the guide\n' > docs/notes.md
$ af submit
Submitted commit 448a7eef3d902915a65af99742efb4487ee2ef4c0535a0c4dd17a7b289a12e20, 2 edits in its last snapshot
```

Once the landing is in the index, each matching line comes back as `path:line:text`:

```console eventually
$ af search TODO
docs/notes.md:3:TODO: write the guide
src/main.rs:2:    // TODO: greet by name
```

## Narrow it by file

`-f` keeps only files whose path matches a second expression:

```console check
$ af search TODO -f '\.rs$'
src/main.rs:2:    // TODO: greet by name
```

With only `-f`, `af search` lists the paths that match, which is a quick way to find files:

```console check
$ af search -f '\.md$'
docs/notes.md
```

The pattern is Rust's regular expression syntax, and `^` and `$` match at the start and end of each line. Results stop at 20 files and 20 lines a file; `-n` and `--lines` change that. A search by path alone gives the first files in path order. A search of text that matches more files than the limit gives some of them, listed in path order, but which ones is not decided by path order. An expression that does not parse is refused, and so is `af search` with neither an expression nor `-f`.

## What search covers

- **Only `main`.** A change that is still open is not searched. The index takes `main` in at most once a second, so a landing is searchable a moment after it lands, and a search straight after a landing can miss it. Search again.
- **Only files you may read.** A folder you may not read gives no result, by text or by path.
- **Only text.** A file is searched only if it is valid UTF-8, has no line longer than 2,000 bytes and is at most a mebibyte. A file that is not text is not in the index at all, so `-f` does not list it either. [Search, caches and releases](https://docs.forge.workreef.dev/concepts/search-caches-releases/#code-search) has the whole rule and says how the index is kept.

## Next

- [`af search`](https://docs.forge.workreef.dev/reference/af/search/) in the reference.
- [Explore history](https://docs.forge.workreef.dev/af/history/): `files()` in a revset finds the commits that changed a path.

Source: https://docs.forge.workreef.dev/af/search/index.mdx
