# pi-reverse-search

Ctrl+R reverse prompt search for [Pi](https://pi.dev)'s TUI editor, inspired by shell `reverse-i-search` in bash/readline and zsh.

## Features

- Press `Ctrl+R` in Pi's input box to enter reverse prompt search.
- Type to filter previous user prompts from the current session branch.
- Matching text is highlighted in the selected prompt.
- Press `Ctrl+R` again to cycle older matches for the current query.
- Press `Ctrl+S`, `Down`, or `Ctrl+N` to cycle newer matches.
- Press `Enter` to accept and submit the selected prompt.
- Press `Esc` or `Ctrl+G` to cancel and restore your draft.
- Press `Ctrl+C` to cancel search and let Pi handle normal clear/interrupt behavior.

## Install

### From a local checkout

```bash
pi install /absolute/path/to/pi-reverse-search
```

Or test without installing:

```bash
pi -e /absolute/path/to/pi-reverse-search/extensions/reverse-search.ts
```

### From GitHub

```bash
pi install git:github.com/viggy28/pi-reverse-search@v0.1.0
```

Or install the latest `main` branch:

```bash
pi install git:github.com/viggy28/pi-reverse-search
```

### From npm

```bash
pi install npm:pi-reverse-search
```

To pin a specific release:

```bash
pi install npm:pi-reverse-search@0.1.0
```

## Usage

1. Start Pi after installing the package, or run `/reload` in an existing session.
2. Press `Ctrl+R` in the prompt editor.
3. Type a search query.
4. Use `Ctrl+R` / `Ctrl+S` to cycle matches.
5. Press `Enter` to submit, or `Esc` / `Ctrl+G` to cancel.

## Development

Run tests:

```bash
npm test
```

Smoke-test extension loading:

```bash
npm run smoke
```

## Package layout

```text
extensions/reverse-search.ts              # Pi extension entrypoint
extensions/reverse-search-lib/core.ts     # Pure search state + key decoding logic
test/reverse-search.test.mjs              # Node tests for core behavior
```

## Troubleshooting

### Extension shortcut conflict for `ctrl+r`

This package does not register a global Pi shortcut; it handles `Ctrl+R` only inside its custom prompt editor. If Pi reports a conflict such as:

```text
Extension shortcut conflict: 'ctrl+r' is built-in shortcut for app.session.rename and /path/to/pi-reverse-search/src/index.ts
```

then Pi is loading an older local checkout that registered `Ctrl+R` as a global shortcut. Remove that local install, then reload Pi and install the npm package instead:

```bash
pi uninstall /path/to/pi-reverse-search
pi install npm:pi-reverse-search
```

If the warning path points at an npm cache/package directory rather than a local `src/index.ts`, update to the latest package:

```bash
pi install npm:pi-reverse-search@latest
```

## Notes

This extension searches prompts in the active Pi session branch. It does not search all historical session files.
