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 vimon 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
- Open the file in Vim. Run
vim filename.txtin your terminal. You’ll land in Normal mode. - Understand the substitution command syntax. The core command is
:[range]s/old/new/[flags]. The colon enters Command-line mode,sstands for substitute, and the slashes separate the search pattern (old) from the replacement (new). - 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. - Replace every occurrence on the current line. Add the
g(global) flag::s/old/new/g. Withoutg, only the first match per line is replaced. - 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. - 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). - Confirm each replacement. Add the
cflag 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). - Use a different delimiter if your text contains slashes. For example, to replace a path like
/usr/binwith/usr/local/bin, use:as the delimiter::%s#/usr/bin#/usr/local/bin#gor:%s!/usr/bin!/usr/local/bin!g. - Match whole words only. Add
\<and\>around the pattern to match word boundaries::%s/\<old\>/new/g. This prevents replacing “old” inside “older”. - Use case-insensitive matching. Add the
iflag::%s/old/new/gi. To make it case-sensitive again, useI(or setignorecaseoff). - 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). - Save your changes. After the replacement, type
:wto write the file. To save and exit, use:wqorZZ.
Common Mistakes
- Forgetting the
gflag. If you only replace the first occurrence on each line, you might miss many matches. Always addgunless you specifically want only the first. - Forgetting the
%range. Typing:s/old/new/gwithout%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
cflag. Use:%s/old/new/gcto review each match before applying. This is safer for large files. - Use
nflag to count matches.:%s/old/new/gnreports the number of occurrences without actually replacing anything. - Search first, then replace. Type
/oldto 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 hlsearchto highlight matches. This makes it easier to see what will be replaced. - Undo mistakes. Press
uin 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
Escto 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 thegflag. - Special characters in the replacement are interpreted. In the replacement part,
&means the whole match, and\1refers 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
- iOS 18 Safari Not Working? Fixes That Help
- How to Check Your Linux Kernel for Spectre
- iPhone Safe Mode: How to Enable or Disable It
- Fix Slow Internet Lag on Samsung Galaxy Note
- How to Get iOS 17 on iPhone X (Safe Methods)
- Fix Gmail IMAP Error in Outlook: Step-by-Step