Home / Text guides / Guide: find and replace

Find and Replace: A Practical Guide

How find and replace works, the options that matter, and how to do it in your editor, spreadsheet or terminal.

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.

Open find and replace

Frequently asked questions

How do I replace text in a file?

Open it in an editor and press Ctrl+H, or use sed on the command line. For quick text, paste it into the online tool.

What is the difference between find and replace and regex replace?

Plain find and replace matches exact text. Regex matches patterns, such as any number or any email address.

How do I avoid replacing part of another word?

Turn on whole-word matching.