---
title: Notebook search
description: 'Notebook-aware search: run `rg`-style parallel search over `.ipynb`
  cell sources.'
order: 2
---
[`nbrg`](rgapi.md#nbrg) searches `.ipynb` cell sources in parallel and returns [`NbCell`](nb.md#nbcell) rows with stable cell IDs, cell types, and `nbdev` directives instead of escaped JSON lines. Outputs and cell metadata are never searched. Invalid regexes or backtracking failures raise `ValueError`, and unreadable or unparseable notebooks are skipped with a `RuntimeWarning`.

```python
nbrg(r"@patch", root="nbs", cell_context=1)
nbrg(r"def foo", root="nbs", paths=True)
hits = await nbrga(r"test_eq", root="nbs")
```

## Searching notebook trees

Use [`nbrg`](rgapi.md#nbrg) for an eager [`NbResults`](nb.md#nbresults) list, [`nbrg_iter`](rgapi.md#nbrg_iter) for lazy iteration, or [`nbrga`](rgapi.md#nbrga) and [`nbrga_iter`](rgapi.md#nbrga_iter) from asyncio code. Pass `cell_context=1` to include neighboring cells, or `multiline=True` to match across lines within a cell.

### nbrg

```python
def nbrg(
    pattern:str, # Regex pattern to search for
    root:str | Path | list='.', # Directory or file to search, or a list of them (expands `~`)
    cell_context:int=0, # Cells of context to include before/after each matching cell
    case_sensitive:bool | None=None, # True/False forces case; None allows `smart_case`
    smart_case:bool=False, # Match `rg --smart-case` behavior
    paths:bool=False, # Return unique matched paths instead of rows
    multiline:bool=False, # Let the pattern match across lines within a cell
    max_results:int | None=None, # Return at most this many cells
    count:bool=False, # Return the number of matching cells instead of results
    timeout_ms:int | None=None, # Cancel the search after this long and return partial results
    maxlen:int=180, # Maximum source characters per displayed cell
    *, glob:str | list | None=None, # Include glob or globs; alias for `include`
    include:str | list | None=None, # Include glob or globs, e.g. `*.py`
    exclude:str | list | None=None, # Exclude glob or globs, e.g. `test_*.py`
    hidden:bool=False, # Include hidden files and directories
    ignore:bool=True, # Respect `.gitignore` and other ignore files
    max_depth:int | None=None, # Maximum directory depth to descend
    min_depth:int | None=None, # Minimum depth for returned/searched paths
    max_filesize:int | None=None, # Skip files larger than this many bytes
    follow_links:bool=False, # Follow symbolic links while walking
    same_file_system:bool=False, # Do not cross filesystem boundaries
    path_re:str | None=None, # Regex that relative paths must match
    skip_path_re:str | None=None, # Regex for relative paths to skip
    skip_dir:str | list | None=None, # Directory glob or globs to prune
    skip_dir_re:str | None=None, # Directory regex used to prune traversal
):
```

*Search `.ipynb` cell sources under `root` in parallel, returning matched cells, paths, or a count.*

Only cell sources are searched, never metadata or outputs. Backreferences and lookaround are line-local by default. `multiline=True` allows matching across lines within a cell, never across cells; `^` and `$` still anchor individual lines. Compilation or backtracking-limit failures raise `ValueError`; timeouts cannot interrupt an in-progress regex evaluation. Metadata `nbdev` directives display as a bracket tag after the cell id (e.g. `nb.ipynb:c1[export]:…`). A match preview starts at the first matched line and marks omitted earlier lines as `…[Ln]` (1-based), keeping any leading `#|` directive line as in `#| export…[L4]needle here`.

### nbrg\_iter

```python
def nbrg_iter(
    pattern:str, # Regex pattern to search for
    root:str | Path | list='.', # Directory or file to search, or a list of them (expands `~`)
    cell_context:int=0, # Cells of context to include before/after each matching cell
    case_sensitive:bool | None=None, # True/False forces case; None allows `smart_case`
    smart_case:bool=False, # Match `rg --smart-case` behavior
    multiline:bool=False, # Let the pattern match across lines within a cell
    maxlen:int=180, # Maximum source characters per displayed cell
    *, glob:str | list | None=None, # Include glob or globs; alias for `include`
    include:str | list | None=None, # Include glob or globs, e.g. `*.py`
    exclude:str | list | None=None, # Exclude glob or globs, e.g. `test_*.py`
    hidden:bool=False, # Include hidden files and directories
    ignore:bool=True, # Respect `.gitignore` and other ignore files
    max_depth:int | None=None, # Maximum directory depth to descend
    min_depth:int | None=None, # Minimum depth for returned/searched paths
    max_filesize:int | None=None, # Skip files larger than this many bytes
    follow_links:bool=False, # Follow symbolic links while walking
    same_file_system:bool=False, # Do not cross filesystem boundaries
    path_re:str | None=None, # Regex that relative paths must match
    skip_path_re:str | None=None, # Regex for relative paths to skip
    skip_dir:str | list | None=None, # Directory glob or globs to prune
    skip_dir_re:str | None=None, # Directory regex used to prune traversal
):
```

*Search `.ipynb` cell sources lazily, yielding [`NbCell`](nb.md#nbcell) rows as they are found.*

### nbrga

```python
async def nbrga(
    pattern:str, # Regex pattern to search for
    root:str | Path | list='.', # Directory or file to search, or a list of them (expands `~`)
    cell_context:int=0, # Cells of context to include before/after each matching cell
    case_sensitive:bool | None=None, # True/False forces case; None allows `smart_case`
    smart_case:bool=False, # Match `rg --smart-case` behavior
    paths:bool=False, # Return unique matched paths instead of rows
    multiline:bool=False, # Let the pattern match across lines within a cell
    max_results:int | None=None, # Return at most this many cells
    count:bool=False, # Return the number of matching cells instead of results
    timeout_ms:int | None=None, # Cancel the search after this long and return partial results
    maxlen:int=180, # Maximum source characters per displayed cell
    *, glob:str | list | None=None, # Include glob or globs; alias for `include`
    include:str | list | None=None, # Include glob or globs, e.g. `*.py`
    exclude:str | list | None=None, # Exclude glob or globs, e.g. `test_*.py`
    hidden:bool=False, # Include hidden files and directories
    ignore:bool=True, # Respect `.gitignore` and other ignore files
    max_depth:int | None=None, # Maximum directory depth to descend
    min_depth:int | None=None, # Minimum depth for returned/searched paths
    max_filesize:int | None=None, # Skip files larger than this many bytes
    follow_links:bool=False, # Follow symbolic links while walking
    same_file_system:bool=False, # Do not cross filesystem boundaries
    path_re:str | None=None, # Regex that relative paths must match
    skip_path_re:str | None=None, # Regex for relative paths to skip
    skip_dir:str | list | None=None, # Directory glob or globs to prune
    skip_dir_re:str | None=None, # Directory regex used to prune traversal
):
```

*Async [`nbrg`](rgapi.md#nbrg): search notebooks on Rust threads without blocking the event loop.*

### nbrga\_iter

```python
def nbrga_iter(
    pattern:str, # Regex pattern to search for
    root:str | Path | list='.', # Directory or file to search, or a list of them (expands `~`)
    cell_context:int=0, # Cells of context to include before/after each matching cell
    case_sensitive:bool | None=None, # True/False forces case; None allows `smart_case`
    smart_case:bool=False, # Match `rg --smart-case` behavior
    multiline:bool=False, # Let the pattern match across lines within a cell
    batch_max:int=512, # Largest batch of cells delivered to the event loop at once
    maxlen:int=180, # Maximum source characters per displayed cell
    *, glob:str | list | None=None, # Include glob or globs; alias for `include`
    include:str | list | None=None, # Include glob or globs, e.g. `*.py`
    exclude:str | list | None=None, # Exclude glob or globs, e.g. `test_*.py`
    hidden:bool=False, # Include hidden files and directories
    ignore:bool=True, # Respect `.gitignore` and other ignore files
    max_depth:int | None=None, # Maximum directory depth to descend
    min_depth:int | None=None, # Minimum depth for returned/searched paths
    max_filesize:int | None=None, # Skip files larger than this many bytes
    follow_links:bool=False, # Follow symbolic links while walking
    same_file_system:bool=False, # Do not cross filesystem boundaries
    path_re:str | None=None, # Regex that relative paths must match
    skip_path_re:str | None=None, # Regex for relative paths to skip
    skip_dir:str | list | None=None, # Directory glob or globs to prune
    skip_dir_re:str | None=None, # Directory regex used to prune traversal
):
```

*Async [`nbrg_iter`](rgapi.md#nbrg_iter): yield [`NbCell`](nb.md#nbcell) rows as they are found; early exit cancels the search.*

## Single-file search and results

[`search_nb`](nb.md#search_nb) searches one `.ipynb` file directly without walking a directory tree.

### search\_nb

```python
def search_nb(
    pattern:str, # Regex pattern to search for
    path, # Notebook file to search (expands `~`)
    cell_context:int=0, # Cells of context to include before/after each matching cell
    case_sensitive:bool | None=None, # True/False forces case; None allows `smart_case`
    smart_case:bool=False, # Match `rg --smart-case` behavior
    display_path:str | Path | None=None, # Path stored in results; defaults to `path`
    maxlen:int=180, # Maximum source characters per displayed cell
)->NbResults:
```

*Search one `.ipynb` file's cell sources with line-local backreferences and lookaround, returning matched cells.*

### NbCell

```python
def NbCell(
    path:str, # Relative path label for the notebook
    cell_index:int, # 0-based index of the cell within the notebook
    cell_id:str, # Stable notebook cell ID
    cell_type:str, # Cell type: `'code'`, `'markdown'`, or `'raw'`
    kind:str, # `'match'` or `'context'`
    source:str, # Full source text of the cell
    matches:list, # Matching `SearchLine` rows within this cell
    maxlen:int=180, # Maximum source characters shown in `str()`
    nbdev:dict=None, # Optional `metadata.nbdev` directives dict
):
```

*A notebook cell that matched (or provides context for) a search.*

Fields: `path`, `cell_index`, `cell_id`, `cell_type` (code, markdown, or raw), `kind` (match or context), the full `source`, `matches` as matching [`SearchLine`](rgapi.md#searchline)s, and `nbdev` (`metadata.nbdev` directives dict). Display is `path:cell_id[directives]:source`, with `-` in place of the last colon on context rows. Newline runs display as ¶, keeping indentation. `maxlen` limits the displayed source, not the stored `source`.

#### NbCell.asdict

```python
def asdict()->dict:
```

*Return all cell fields (with `matches` converted via `asdict()`) as a plain `dict`.*

### NbResults

```python
def NbResults(
    *args, **kwargs
):
```

*List of [`NbCell`](nb.md#nbcell) rows with rg-style text display.*
