sodcalculator

10 Tech How-To

Find and Replace Text in Vim on Linux

Guide 10-46811 · Updated · Step by step

This guide shows you how to find and replace text in Vim, the default terminal-based text editor on most Linux distributions. Whether you are editing a config file or a script, you’ll learn the exact commands to substitute a word, phrase, or pattern across a single line, the whole file, or a selected range of lines. No prior Vim experience is needed, but you should know how to open a file in Vim (e.g., vim filename.txt) and be comfortable with basic navigation.

What You’ll Need

  • A Linux system with Vim installed (most distributions include it; if not, install it with your package manager, e.g., sudo apt install vim on Debian/Ubuntu).
  • A text file you want to edit.
  • Basic familiarity with Vim’s modes: Normal mode (default) and Command-line mode (accessed by typing :).

Step-by-Step

  1. Open the file in Vim. Run vim filename.txt in your terminal. You’ll land in Normal mode.
  2. Understand the substitution command syntax. The core command is :[range]s/old/new/[flags]. The colon enters Command-line mode, s stands for substitute, and the slashes separate the search pattern (old) from the replacement (new).
  3. Replace the first occurrence on the current line. Move the cursor to the line you want, then type :s/old/new/ and press Enter. This changes only the first match on that line.
  4. Replace every occurrence on the current line. Add the g (global) flag: :s/old/new/g. Without g, only the first match per line is replaced.
  5. Replace in the entire file. Use % as the range to mean “all lines”: :%s/old/new/g. This is the most common find-and-replace operation.
  6. Replace in a specific range of lines. For lines 10 through 20, type :10,20s/old/new/g. You can also use relative ranges like :.,+5s/old/new/g (current line to five lines down).
  7. Confirm each replacement. Add the c flag to ask for confirmation before each change: :%s/old/new/gc. Vim will show the match and prompt you with options: y (yes), n (no), a (all remaining), q (quit), l (change this one and quit).
  8. Use a different delimiter if your text contains slashes. For example, to replace a path like /usr/bin with /usr/local/bin, use : as the delimiter: :%s#/usr/bin#/usr/local/bin#g or :%s!/usr/bin!/usr/local/bin!g.
  9. Match whole words only. Add \< and \> around the pattern to match word boundaries: :%s/\<old\>/new/g. This prevents replacing “old” inside “older”.
  10. Use case-insensitive matching. Add the i flag: :%s/old/new/gi. To make it case-sensitive again, use I (or set ignorecase off).
  11. Replace using a regular expression. Vim’s search patterns are powerful. For example, to replace all digits with “#”, use :%s/\d/#/g. To match any character, use . (escaped as \. if you literally want a dot).
  12. Save your changes. After the replacement, type :w to write the file. To save and exit, use :wq or ZZ.

Common Mistakes

  • Forgetting the g flag. If you only replace the first occurrence on each line, you might miss many matches. Always add g unless you specifically want only the first.
  • Forgetting the % range. Typing :s/old/new/g without % only affects the current line, not the whole file.
  • Not escaping special characters. Characters like ., *, [, and ] have special meanings in patterns. If you want a literal dot, use \.. For a literal slash, change the delimiter.
  • Using the wrong delimiter. If your search or replacement contains a slash, the command will fail or behave unexpectedly. Use # or ! instead.
  • Confusing Vim’s command mode with insert mode. You must be in Normal mode (press Esc) before typing the colon.

Tips

  • Preview changes with c flag. Use :%s/old/new/gc to review each match before applying. This is safer for large files.
  • Use n flag to count matches. :%s/old/new/gn reports the number of occurrences without actually replacing anything.
  • Search first, then replace. Type /old to search and highlight matches. Then you can run the substitution with the same pattern by using an empty search field: :%s//new/g.
  • Use set hlsearch to highlight matches. This makes it easier to see what will be replaced.
  • Undo mistakes. Press u in Normal mode to undo the last change. You can undo multiple times.
  • Practice on a copy. If you’re unsure, copy the file and test the command on the copy first.

Troubleshooting

  • “E486: Pattern not found” – This means the search text does not exist in the specified range. Check the spelling, case, and whether you used the correct range. Remember that the pattern is case-sensitive by default.
  • “E492: Not an editor command” – This usually means you typed the command in the wrong mode. Press Esc to ensure you’re in Normal mode, then type the colon.
  • Replacement seems to do nothing. Check if you used the % range. If you only see a change on one line, you likely omitted it. Also verify the g flag.
  • Special characters in the replacement are interpreted. In the replacement part, & means the whole match, and \1 refers to a capture group. To insert a literal ampersand, escape it as \&.
  • Vim says “Pattern not found” but I see the text. The text might be in a part of the file not covered by your range. For example, if you’re on line 1 and use :s/old/new/g, it only affects line 1. Use % to cover the whole file.

Frequently Asked Questions

How do I replace all occurrences of a word in the entire file?

Type :%s/word/new/g. The % means all lines, g means every occurrence on each line.

How do I replace text and ask for confirmation each time?

Add the c flag: :%s/old/new/gc. Vim will prompt you for each match.

Can I replace text without changing case?

Yes, use the i flag for case-insensitive matching: :%s/old/new/gi. To preserve the case of the original, you need to use capture groups, but that’s more advanced.

How do I replace a slash (/) in text?

Use a different delimiter, such as # or !. Example: :%s#/old/path#/new/path#g.

What does the g flag do exactly?

It makes the substitution replace every occurrence on each line. Without it, only the first match per line is replaced.

How do I undo a replacement?

Press u in Normal mode to undo the last change. You can press it multiple times to undo several changes.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →