When working with Vim or Vi editors, one of the most essential skills is mastering find and replace. Whether you're editing configuration files on a server or working with code across multiple files, knowing how to find and replace in Vim and Vi can significantly improve your productivity.
In this guide, we’ll show you exactly how to find and replace in Vim and Vi, including different scopes like a single line, the entire file, across multiple buffers, and even in a specific range or selection. We'll also walk you through replacing with confirmation, dealing with special characters, and using command-line options. Whether you're new to Vim or an experienced developer, this guide will help you master Vim’s powerful substitution tools.
Understanding Find and Replace in Vim and Vi
Before jumping into how to find and replace in Vim and Vi, let’s clarify some basics. Both Vim and Vi are modal text editors available on Unix-based systems. Vi is the original editor, while Vim stands for “Vi Improved.” Although Vim includes many enhancements, the core substitution (find and replace) syntax is the same.
In both editors, the command to search and replace generally follows this pattern:
:[range]s/pattern/replacement/[flags]
Let’s break it down:
-
range: Optional. Specifies where the substitution should occur (e.g., a line number, visual selection, or % for the whole file).
-
pattern: The text you want to find.
-
replacement: The text you want to insert instead.
-
flags: Control how substitution works, such as g for all occurrences, c for confirmation, etc.
How to Find and Replace a String in Vim
Vim provides powerful tools to search and replace strings, starting from simple line-based replacements. If you want to replace just the first occurrence of a particular string within the current line, you can use the following command:
:s/old/new/
Here’s what each part of the command means:
-
: enters command mode.
-
s stands for substitution.
-
old is the text you want to search for.
-
new is the replacement text.
This command will search the current line for the first instance of the word "old" and replace it with "new". Only the first match per line will be replaced.
If your goal is to replace all occurrences of the string "old" on the current line, you can make the command global by appending the g flag:
:s/old/new/g
The g flag tells Vim to continue the substitution throughout the entire line rather than stopping after the first match. This is especially useful when the same word appears multiple times on one line.
How to Find and Replace Text Throughout the Entire File in Vim
When you need to replace a word or phrase across the entire file, Vim makes it easy with the :%s command. Here’s the syntax:
:%s/old/new/g
Explanation:
-
% indicates that the substitution should happen across every line in the file.
-
s still stands for substitution.
-
old is the string you want to replace.
-
new is the replacement.
-
g ensures all instances in each line are replaced (not just the first).
Example:
:%s/localhost/127.0.0.1/g
This command will search every line in the current file and replace all instances of "localhost" with "127.0.0.1". It's a very efficient way to make global changes without manually editing each line.
How to Search and Replace in Vim with Confirmation
Sometimes you want more control over your replacements—especially if you're not sure about changing every match. In such cases, Vim allows you to confirm each substitution individually by using the c (confirm) flag:
:%s/old/new/gc
This command does everything the previous one does, but it will prompt you each time it finds a match. You'll see:
replace with new (y/n/a/q/l/^E/^Y)?
Here’s what the options mean:
-
y – replace this match
-
n – skip this match
-
a – replace all remaining matches
-
q – quit substitution
-
l – replace this match and then quit
-
^E – scroll down
-
^Y – scroll up
Using confirmation mode is ideal when you want to double-check what’s being replaced without committing to every change automatically.
Find and Replace in Vi Editor on Linux
If you're using the older Vi editor (the predecessor to Vim) on a Linux system, you'll be glad to know that the basic search and replace functionality is still available—and the commands are nearly identical.
For example, to perform a global substitution in Vi:
:%s/target/replacement/g
Even though Vi lacks some modern conveniences like visual mode or syntax highlighting, the substitution syntax works just like it does in Vim. This makes it easy to transition between the two editors or to work on older systems where only Vi is installed.
If you're looking for more guidance specific to Vi, check out our full guide on the Vi Linux Text Editor for tips and examples.
Find and Replace Special Characters in Vi or Vim
Certain characters—such as slashes (/), dots (.), parentheses (()), and others—are interpreted specially in regular expressions. If you want to match them literally in your search and replace, you’ll need to escape them with a backslash (\).
For example:
:%s/\./-/g
This command replaces all periods (.) with hyphens (-). Without the backslash, Vim would treat the . as a wildcard that matches any single character.
To replace a full URL with another, escaping becomes even more necessary:
:%s/http:\/\/example\.com/https:\/\/example.com/g
However, escaping slashes in long URLs can get messy. Fortunately, Vim allows you to use alternative delimiters. You can use characters like #, |, or @ instead of /:
:%s#http://#https://#g
This version is easier to read and avoids backslashes, which makes your command cleaner—especially useful when working with complex patterns or URLs.
How to find and replace in Vim and Vi Within a Specific Range
You may not always want to perform a substitution across the entire file. Vim allows you to limit your search and replace operation to a specific range of lines.
Here’s how to do it:
:10,20s/foo/bar/g
Explanation:
-
10,20 defines the line range—from line 10 to line 20.
-
s/foo/bar/g replaces every occurrence of "foo" with "bar" in that range.
This is especially useful when working with configuration files, code blocks, or logs where you only want to make changes within a defined section of your document.
You can also use relative or special markers:
-
:.,$s/foo/bar/g → Replace from current line (.) to end of file ($)
-
:1,.s/foo/bar/g → Replace from top of file to current line
Vim Search and Replace in Selection
To replace text in a selected visual block:
-
Enter visual mode by pressing v.
-
Select the desired text.
-
Type : and it will automatically populate a range like :'<,'>.
-
Complete the substitution:
:'<,'>s/foo/bar/g
This is great when you don’t want to affect the rest of the file.
Vim Find and Replace in All Buffers
If you're working across multiple open files (buffers), you can use the :bufdo command:
:bufdo %s/foo/bar/g | update
This performs a global find and replace in every buffer, and then saves (update) the changes.
How to find and replace in Vim and Vi Entire File
To reiterate, replacing across the entire file is done using:
:%s/old/new/g
Adding the c flag gives you control:
:%s/old/new/gc
You can also combine it with regular expressions for advanced matches.
Vim Replace Word Only
To avoid partial matches, use the \b word boundary:
:%s/\bold\b/new/g
This will replace old but not folder or boldly.
Vi Replace String in Multiple Lines
If you're selecting or working in a specific range:
:45,90s/target/replacement/g
This replaces only between lines 45 and 90.
To replace over several lines visually:
-
Press V to start line-wise selection.
-
Move the cursor to extend the selection.
-
Type :s/foo/bar/g
Vim will pre-fill the correct range.
How to Do Find and Replace in Vim Command Line
You can start Vim and directly execute a substitution using +:
vim +'%s/foo/bar/g | wq' file.txt
This opens the file, performs the substitution, writes (w) the changes, and quits (q).
This is especially useful in scripts or batch jobs.
Vim Find and Replace in Directory
To replace text in multiple files in a directory:
vim `grep -rl 'foo' ./path` +':%s/foo/bar/g | wq'
Or use Vim's :args:
:args **/*.txt
:argdo %s/foo/bar/g | update
This performs the command across all matched files.
Need more Linux command-line tips? Check out our Linux VPS Hosting for performance and flexibility.
Best Practices for Vim Search and Replace
Now that you know how to find and replace in Vim and Vi, let’s review some of the best practices that you can use:
-
Use the c flag to avoid accidental replacements.
-
Always escape special characters.
-
Use :w !sudo tee % to save read-only files.
-
Learn regular expressions (.*, \d, \b, etc.) for powerful pattern matching.
Final Thoughts
Learning how to find and replace in Vim and Vi unlocks one of the most efficient features in these powerful editors. Whether you’re doing a global find and replace in Vi, editing files on a remote server, or working with multiple files in Vim, the substitution command is your best friend.
Use flags like g, c, and i smartly, and don't be afraid to harness the power of regex for advanced replacements. With practice, you'll find that Vim can be just as productive—if not more—than modern GUI-based editors.
For more tips and tools to power up your text editing experience, check out: