> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/sinelaw/fresh/llms.txt
> Use this file to discover all available pages before exploring further.

# Search and Replace

> Find and replace text with regular expressions, case sensitivity, whole word matching, and interactive query replace

Fresh provides powerful search and replace functionality for both single files and entire projects, with support for regular expressions, capture groups, and interactive replacement.

## Quick Search

The fastest way to search in the current buffer:

| Shortcut   | Action              |
| ---------- | ------------------- |
| `Ctrl+F`   | Open search prompt  |
| `F3`       | Find next match     |
| `Shift+F3` | Find previous match |
| `Esc`      | Close search prompt |

<Steps>
  <Step title="Open search">
    Press `Ctrl+F` to open the search toolbar at the bottom of the screen
  </Step>

  <Step title="Enter search term">
    Type the text you want to find
  </Step>

  <Step title="Navigate matches">
    Use `F3` to jump to the next match, or `Shift+F3` for the previous match
  </Step>

  <Step title="Close search">
    Press `Esc` to close the search prompt
  </Step>
</Steps>

## Search and Replace

Replace text in the current buffer:

| Shortcut     | Action                                             |
| ------------ | -------------------------------------------------- |
| `Ctrl+R`     | Open search and replace prompt                     |
| `Ctrl+Alt+R` | Interactive query replace (y/n/!/q for each match) |

<Steps>
  <Step title="Open replace">
    Press `Ctrl+R` to open the replace toolbar
  </Step>

  <Step title="Enter search and replacement">
    Type the search term, then Tab to the replacement field
  </Step>

  <Step title="Replace">
    Press Enter to replace the next match, or use "Replace All" to replace all occurrences
  </Step>
</Steps>

## Search Options

The search toolbar includes toggle buttons for fine-tuning your search:

<CardGroup cols={3}>
  <Card title="Case Sensitive" icon="font-case">
    Match exact uppercase/lowercase. When disabled, `foo` matches `Foo`, `FOO`, and `foo`.
  </Card>

  <Card title="Whole Word" icon="text-width">
    Match complete words only. When enabled, `cat` won't match `category` or `scat`.
  </Card>

  <Card title="Regex" icon="brackets-square">
    Use regular expressions for pattern matching. Enables powerful search patterns and capture groups.
  </Card>
</CardGroup>

<Tip>
  Click the toggle buttons in the search toolbar, or use keyboard shortcuts to toggle them (shown in the toolbar).
</Tip>

## Regular Expressions

Enable regex mode for powerful pattern matching:

### Basic Regex Patterns

<CodeGroup>
  ```regex Word boundaries theme={null}
  \bfunction\b
  # Matches "function" as a complete word only
  ```

  ```regex Character classes theme={null}
  [0-9]+
  # Matches one or more digits
  ```

  ```regex Wildcards theme={null}
  log.*Error
  # Matches "log" followed by anything, then "Error"
  ```

  ```regex Alternation theme={null}
  (TODO|FIXME|NOTE)
  # Matches TODO, FIXME, or NOTE
  ```
</CodeGroup>

### Capture Groups and Replacement

When regex mode is enabled, you can use capture groups in your replacement string:

<Tabs>
  <Tab title="Numbered Groups">
    Use `$1`, `$2`, etc. to reference captured groups:

    **Search:** `(\w+): (\w+)`\
    **Replace:** `$2: $1`\
    **Result:** `name: value` → `value: name`
  </Tab>

  <Tab title="Named Groups">
    Use `${name}` syntax for named capture groups:

    **Search:** `(?<key>\w+): (?<val>\w+)`\
    **Replace:** `${val}: ${key}`\
    **Result:** `name: value` → `value: name`
  </Tab>
</Tabs>

### Regex Examples

<AccordionGroup>
  <Accordion title="Swap function parameters">
    **Search:** `function (\w+)\((\w+), (\w+)\)`\
    **Replace:** `function $1($3, $2)`

    Swaps the order of two function parameters.
  </Accordion>

  <Accordion title="Convert camelCase to snake_case">
    **Search:** `([a-z])([A-Z])`\
    **Replace:** `$1_$2`

    Then convert to lowercase separately.
  </Accordion>

  <Accordion title="Add quotes around words">
    **Search:** `\b(\w+)\b`\
    **Replace:** `"$1"`

    Wraps each word in double quotes.
  </Accordion>

  <Accordion title="Extract domain from URLs">
    **Search:** `https?://([^/]+)/.*`\
    **Replace:** `$1`

    Extracts just the domain from full URLs.
  </Accordion>
</AccordionGroup>

## Interactive Query Replace

For precise control over which matches to replace, use query replace:

<Steps>
  <Step title="Start query replace">
    Press `Ctrl+Alt+R` or use "Query Replace" from the command palette
  </Step>

  <Step title="Enter search and replacement">
    Type the search term and replacement text
  </Step>

  <Step title="Decide for each match">
    Fresh prompts for each match:

    * `y` — Replace this match
    * `n` — Skip this match
    * `!` — Replace all remaining matches
    * `q` — Quit query replace
  </Step>
</Steps>

<Tip>
  Query replace is perfect when you want to replace most occurrences but skip a few specific ones.
</Tip>

## Find Selection

Quickly find the next occurrence of the currently selected text:

| Shortcut                   | Action                                |
| -------------------------- | ------------------------------------- |
| `Alt+N` or `Ctrl+F3`       | Find next occurrence of selection     |
| `Alt+P` or `Ctrl+Shift+F3` | Find previous occurrence of selection |

<Steps>
  <Step title="Select text">
    Use mouse or keyboard to select a word or phrase
  </Step>

  <Step title="Find next">
    Press `Alt+N` or `Ctrl+F3` to jump to the next occurrence
  </Step>

  <Step title="Create multi-cursor (optional)">
    Use `Ctrl+D` to add a cursor at the next match for multi-cursor editing
  </Step>
</Steps>

<Note>
  This is different from `Ctrl+D` which adds a cursor at the next match — find selection just moves the cursor without creating additional cursors.
</Note>

## Project-Wide Search

Search across all files in your project using git grep:

<Steps>
  <Step title="Open project search">
    Open command palette (`Ctrl+P`) and search for "Search and Replace in Project"
  </Step>

  <Step title="Enter search term">
    Type the text or regex pattern to search for
  </Step>

  <Step title="View results">
    Fresh searches all git-tracked files and displays matches with file locations
  </Step>

  <Step title="Navigate to match">
    Click or select a result to jump to that location
  </Step>
</Steps>

<Note>
  **Git Integration**: Project search uses `git grep` and only searches files tracked by git. Untracked and ignored files are excluded.
</Note>

### Git Grep Features

Fresh's project search leverages git grep for fast, efficient searching:

<CardGroup cols={2}>
  <Card title="Fast" icon="bolt">
    Git grep is highly optimized and can search large repositories quickly
  </Card>

  <Card title="Respects .gitignore" icon="filter">
    Automatically excludes files in `.gitignore`, `node_modules`, build artifacts, etc.
  </Card>

  <Card title="Regex Support" icon="code">
    Use regular expressions for complex search patterns
  </Card>

  <Card title="Context Aware" icon="file-code">
    Shows surrounding lines for context in search results
  </Card>
</CardGroup>

## Search in Selection

Limit your search to just the selected text:

<Steps>
  <Step title="Select region">
    Use mouse or keyboard to select the text region you want to search within
  </Step>

  <Step title="Open search">
    Press `Ctrl+F` to open search
  </Step>

  <Step title="Search">
    Your search will be limited to the selected region
  </Step>
</Steps>

<Tip>
  This is useful for searching within a specific function, class, or block of code without getting results from the entire file.
</Tip>

## Search Tips and Tricks

<AccordionGroup>
  <Accordion title="Use multi-cursor with find next">
    Instead of replace all, use `Ctrl+D` (add cursor at next match) to create multiple cursors at each occurrence. This gives you visual feedback and more control — you can edit each occurrence differently if needed, and undo is a single operation.
  </Accordion>

  <Accordion title="Combine whole word with regex">
    Enable both "Whole Word" and "Regex" to match patterns at word boundaries only. For example, `\d+` with whole word enabled matches standalone numbers but not digits within words.
  </Accordion>

  <Accordion title="Test regex before replacing">
    Use `Ctrl+F` with regex mode first to verify your pattern matches what you expect. Then use `Ctrl+R` to replace.
  </Accordion>

  <Accordion title="Use query replace for risky changes">
    When making potentially breaking changes (like renaming variables), use `Ctrl+Alt+R` (query replace) to review each match before replacing.
  </Accordion>

  <Accordion title="Search for empty lines">
    Use regex mode and search for `^$` to find empty lines. Replace with nothing to remove them.
  </Accordion>
</AccordionGroup>

## Advanced Regex Patterns

<CodeGroup>
  ```regex Lookahead theme={null}
  function(?=\()
  # Matches "function" only if followed by "("
  ```

  ```regex Lookbehind theme={null}
  (?<=\blet )\w+
  # Matches words that come after "let "
  ```

  ```regex Non-capturing groups theme={null}
  (?:TODO|FIXME): (.+)
  # Groups TODO|FIXME but only captures the text after :
  ```

  ```regex Lazy quantifiers theme={null}
  <div>(.*?)</div>
  # Matches shortest possible content between <div> tags
  ```
</CodeGroup>

## Search Performance

<Tip>
  **Large Files**: Fresh handles search efficiently even in multi-gigabyte files. Regex searches may be slower than literal string searches, but Fresh maintains low latency through incremental searching.
</Tip>

<Note>
  **Project Search**: Uses git grep which is highly optimized. First search may be slower as git indexes, but subsequent searches are very fast.
</Note>

## Related Documentation

<CardGroup cols={3}>
  <Card title="Multi-Cursor Editing" icon="arrows-split-up-and-left" href="/features/multi-cursor">
    Use Ctrl+D to add cursors at matches
  </Card>

  <Card title="Editing Features" icon="pen-to-square" href="/features/editing">
    Learn about text manipulation
  </Card>

  <Card title="Command Palette" icon="terminal" href="/features/command-palette">
    Access all search commands
  </Card>
</CardGroup>
