The core idea
Find and replace searches a text for a pattern and swaps every match for something else. It is the fastest way to fix a repeated mistake, rename something, or reshape data.
The options that matter
- Case: whether Apple and apple count as the same.
- Whole word: whether cat may match inside category.
- Replace all or one: every match, or one at a time with a check.
- Literal or regex: exact text, or a pattern.
In common programs
- Microsoft Word, Excel and Google Docs: press Ctrl+H.
- VS Code: Ctrl+H in a file, Ctrl+Shift+H across a whole project. Use the buttons for case, whole word and regex.
- Notepad++: Ctrl+H, with an option for regular expressions.
On the command line
# Linux and macOS: replace in a file
sed -i 's/old/new/g' file.txt
# PowerShell
(Get-Content file.txt) -replace 'old', 'new' | Set-Content file.txt
# Python
text.replace("old", "new")
re.sub(r"\d+", "#", text)The sed option -i edits the file in place, so keep a backup first. On macOS, the form is sed -i with an empty backup suffix argument.
Common mistakes
- Replacing part of a longer word: use whole-word matching.
- Unescaped special characters: in regex mode a dot matches any character, so write \. for a real full stop.
- Order problems: when running several replacements, an earlier one can change what a later one sees. Swaps need an all-at-once approach.
- No backup: always keep the original until you have checked the result.
Try it here
Use find and replace for a single change, find and replace multiple for a list, regex find and replace for patterns, and case-insensitive replace to keep capitalisation.
Try it. Find and replace in your browser, with highlighted matches and a live result.