To efficiently find and replace text across multiple files in Sublime Text, utilize its powerful Find in Files feature. This tool allows you to search and replace text across as many files as you have open or select from its menu, making large-scale text manipulation straightforward.
Accessing the Find in Files Panel
The primary way to initiate a multi-file find and replace operation is through a simple keyboard shortcut:
- macOS: Press
Command + Shift + F
- Windows/Linux: Press
Control + Shift + H
This will open the "Find in Files" panel, typically at the bottom of your Sublime Text window.
Step-by-Step Guide to Find and Replace
Once the Find in Files panel is open, follow these steps to perform your search and replace operation:
- Enter Your Search Term: In the Find field, type the text you want to locate.
- Enter Your Replacement Term: In the Replace field, type the text you want to use as a substitute. If you only want to find and not replace, leave this field empty.
- Specify Where to Search: The Where field is crucial for defining the scope of your search. Here are common options:
<open files>
: Searches all files currently open in Sublime Text.<current file>
: Limits the search to the active file.<project>
: Searches all files within your active Sublime Text project.- Specific Folder Path: Enter a folder path (e.g.,
/Users/YourName/Projects/MyProject
orC:\Dev\MyProject
). - Multiple Paths: Separate paths with commas.
- Exclusions: Add
-*.log
,-folder_name/
to exclude specific files or directories.
- Configure Search Options (Optional): Use the icons to the left of the Find and Replace fields to refine your search:
Ab
: Case Sensitive (matches text with exact casing)..*
: Regular Expression (allows for complex pattern matching).[]
: Whole Word (matches only complete words, not parts of words).>
: In Selection (searches only within selected text in the current file, if applicable).
- Execute the Search or Replace:
- Click the Find button to preview all occurrences without making changes. The results will appear in a new tab.
- Click the Replace button to perform the replacement across all matching files and instances within the specified scope. A confirmation dialog will appear, indicating the number of files and replacements.
Understanding the "Find in Files" Panel Options
The "Find in Files" panel provides a robust set of options to tailor your search and replace operations:
Feature | Icon | Description |
---|---|---|
Case Sensitive | Ab |
Distinguishes between uppercase and lowercase letters (e.g., "Word" is different from "word"). |
Regular Expression | .* |
Enables advanced pattern matching using regular expression syntax. Essential for complex find and replace tasks. |
Whole Word | [] |
Matches only complete words. "find" will not match "finding" or "finder". |
In Selection | > |
Limits the search to the currently highlighted text in the active file. (Less common for multi-file operations but useful for refinement). |
Where Field | Defines the scope of the search. Can include specific folders, project files, or open files. |
Advanced Usage Tips
- Regular Expressions: For complex replacements, mastering regular expressions (
regex
) is invaluable. Sublime Text's regex engine is powerful and allows for capturing groups, backreferences, and more. For example, to changefunction(param1, param2)
tofunction_new(param1, param2)
, you could usefunction\((.*)\)
in Find andfunction_new(\1)
in Replace. - Excluding Files/Folders: In the Where field, you can specify folders or file types to exclude. For instance,
C:\MyProject,-*.bak,-node_modules/
would searchMyProject
but ignore.bak
files and thenode_modules
directory. - Confirming Changes: Always perform a Find first to review the results before committing to a Replace. This helps prevent unintended modifications.
- Sublime Project Files (
.sublime-project
): For frequently repeated multi-file find and replace operations, especially within specific project structures, defining yourWhere
scopes in your project file can streamline the process.
For more detailed information and advanced configurations, refer to the official Sublime Text Documentation.