← Back to DevBytes

Zed Find and Replace: Complete Guide

Introduction to Zed Find and Replace

Zed is a high-performance, multiplayer code editor built in Rust by the creators of Atom. One of its most powerful features is the Find and Replace system, which allows developers to navigate, search, and modify code across files with remarkable speed. Whether you are refactoring a single function or renaming a variable across an entire project, understanding Zed's Find and Replace capabilities is essential for productive development.

This guide covers everything from basic single-file searches to advanced multi-file replacements using regular expressions, project-wide searches, and best practices to keep your workflow efficient and safe.

Why Find and Replace Matters

Find and Replace is one of the most frequently used operations in any code editor. In a modern development workflow, you might need to:

Doing these tasks manually is error-prone and time-consuming. Zed's Find and Replace tooling makes these operations fast, accurate, and reversible, especially when combined with version control systems like Git.

Opening the Find and Replace Panel

Zed provides keyboard shortcuts to open the Find and Replace panel quickly. The shortcuts differ slightly depending on your operating system.

Single File Search

To search within the currently open file, use the following shortcuts:

This opens the Find bar at the top of the editor pane. To enable the Replace field within the same panel, use:

Project-Wide Search

For searching across the entire project, use the project search panel:

This opens a dedicated panel where you can enter both a search query and a replacement string, along with filters for file paths and inclusion or exclusion patterns.

Basic Single-File Find and Replace

The simplest use case is finding a word in the current file and replacing it. Once the Find panel is open, type your search term. Zed highlights all matches in the editor and shows the number of occurrences in the panel.

Consider the following JavaScript file:

function calculateTotal(price, tax) {
  const total = price + price * tax;
  return total;
}

function calculateDiscount(price, discount) {
  const total = price - price * discount;
  return total;
}

If you want to replace every occurrence of price with basePrice, open the Replace panel, enter price in the Find field and basePrice in the Replace field, then click Replace All. The result will be:

function calculateTotal(basePrice, tax) {
  const total = basePrice + basePrice * tax;
  return total;
}

function calculateDiscount(basePrice, discount) {
  const total = basePrice - basePrice * discount;
  return total;
}

Navigation Shortcuts

While the Find panel is open, you can navigate between matches using these shortcuts:

Search Options and Modifiers

Zed provides several toggleable modifiers in the Find panel. These appear as small icons or buttons next to the search input. Each modifier changes how the search behaves.

Case Sensitivity

When enabled, the search distinguishes between uppercase and lowercase letters. For example, searching for User will not match user or USER. This is useful when working with languages where casing carries semantic meaning, such as TypeScript or Java.

Whole Word Match

This modifier restricts matches to complete words only. Searching for count with whole word matching enabled will not match counter, account, or discount. This is particularly helpful when renaming variables that share substrings with other identifiers.

Regular Expressions

Enabling regex mode allows you to use pattern-based searches. This is the most powerful search mode and is covered in detail in the next section.

Using Regular Expressions

Regular expressions unlock advanced search patterns. To enable regex mode, click the regex icon in the Find panel or press Cmd+Option+R / Alt+R.

Capturing Groups in Replacements

One of the most useful regex features is capturing groups. You can capture parts of the matched text and reference them in the replacement string using $1, $2, and so on.

Suppose you have the following CSS-like configuration:

color: #ff0000;
background: #00ff00;
border-color: #0000ff;

You want to convert these hex colors to CSS variables. Use the following regex in the Find field:

(\w+):\s*(#[0-9a-fA-F]{6});

And use this in the Replace field:

$1: var(--color-$1);

The result will be:

color: var(--color-color);
background: var(--color-background);
border-color: var(--color-border-color);

This example demonstrates the mechanics, though in practice you would refine the pattern to produce cleaner variable names. The key takeaway is that captured groups let you restructure matched text dynamically.

Common Regex Patterns

Here are some practical regex patterns you can use in Zed:

Removing All Console Logs

A common refactoring task is removing debug statements before production. Given this code:

function processData(data) {
  console.log("Processing data", data);
  const result = transform(data);
  console.warn("Result", result);
  return result;
}

Use this regex in the Find field:

console\.(log|warn|error|debug|info)\(.*?\);?\n?

Leave the Replace field empty and click Replace All. The result will be:

function processData(data) {
  const result = transform(data);
  return result;
}

Project-Wide Find and Replace

Project-wide search is where Zed's performance shines. Because Zed is built in Rust, it can search large codebases almost instantly. The project search panel includes additional fields for filtering which files to search.

File Include and Exclude Filters

In the project search panel, you can specify glob patterns to include or exclude files. For example:

These filters help you narrow down results and avoid unintended replacements in configuration files, build artifacts, or test snapshots.

Replacing Across the Project

When performing a project-wide replacement, Zed shows a preview of every file that will be changed. You can review each match individually and choose to include or exclude it from the replacement. This is critical for avoiding unintended changes.

For example, suppose you want to rename an API endpoint from /api/v1/users to /api/v2/users across your project. Enter the old path in the Find field and the new path in the Replace field. Zed will list every file containing the old path. Review the matches, then apply the replacement.

Practical Example: Renaming a Function

Imagine you have a utility function named fetchData that you want to rename to fetchResource across your project. Open the project search panel with Cmd+Shift+F, enable whole word matching, enter fetchData as the search term and fetchResource as the replacement, then review and apply.

Before replacement:

// utils/api.js
export async function fetchData(url) {
  const response = await fetch(url);
  return response.json();
}

// components/UserList.js
import { fetchData } from "../utils/api";

export async function loadUsers() {
  return fetchData("/api/users");
}

// tests/api.test.js
test("fetchData returns JSON", async () => {
  const data = await fetchData("/mock/users");
  expect(data).toBeDefined();
});

After project-wide replacement:

// utils/api.js
export async function fetchResource(url) {
  const response = await fetch(url);
  return response.json();
}

// components/UserList.js
import { fetchResource } from "../utils/api";

export async function loadUsers() {
  return fetchResource("/api/users");
}

// tests/api.test.js
test("fetchResource returns JSON", async () => {
  const data = await fetchResource("/mock/users");
  expect(data).toBeDefined();
});

Preserve Case Replacement

Zed supports case-preserving replacements, which are invaluable when renaming identifiers that appear in different casing conventions. For example, if you rename userAccount to memberAccount, you may also want UserAccount to become MemberAccount and USER_ACCOUNT to become MEMBER_ACCOUNT.

While the exact availability of this feature depends on your Zed version, the general approach is to perform multiple targeted replacements or use regex with case-insensitive matching combined with capturing groups. Always review the preview before applying.

Best Practices

Always Preview Before Replacing

Before clicking Replace All, review the highlighted matches in the editor or the project search results panel. A single unintended match can introduce subtle bugs that are difficult to trace.

Commit Before Large Replacements

If you are using Git, commit your current changes before performing a large project-wide replacement. This gives you a clean restore point. If the replacement introduces issues, you can revert with git checkout or git restore.

git add -A
git commit -m "Snapshot before project-wide rename"

Use Whole Word Matching for Identifiers

When renaming variables, functions, or classes, enable whole word matching to avoid partial matches. For example, replacing user without whole word matching would also change username, userList, and superuser.

Combine Regex with File Filters

For complex refactors, combine regex patterns with file include and exclude filters. This narrows the scope of the search and reduces the risk of unintended changes. For example, when updating import paths, restrict the search to *.{js,ts,jsx,tsx} files.

Test After Replacement

After any significant replacement, run your test suite to verify that nothing broke. This is especially important for replacements involving regex, where edge cases can produce unexpected results.

npm test

Avoid Overly Broad Regex Patterns

Regex patterns like .* or .+ can match far more than intended. Be specific with your patterns and use anchors like ^ and $ when appropriate. Test your regex on a small subset of files before applying it project-wide.

Keyboard Shortcut Reference

Here is a summary of the most important Find and Replace shortcuts in Zed:

Conclusion

Zed's Find and Replace system is a fast, flexible, and powerful tool that scales from simple single-file edits to complex project-wide refactors. By mastering the basic search modifiers, regular expressions, file filters, and keyboard shortcuts, you can dramatically speed up your development workflow while minimizing the risk of unintended changes. Always preview your replacements, commit before large refactors, and run your tests afterward. With these practices in place, Find and Replace becomes one of the most reliable tools in your daily coding routine.

— Ad —

Google AdSense will appear here after approval

← Back to all articles