Introduction
Manipulating text and making multiple edits at once is crucial when working in Vim or its older version Vi.
As a popular Linux text editor, Vim offers several practical ways to find and replace terms quickly and efficiently.
This tutorial shows how to use the find and replace function in Vim/Vi and keep track of all the changes.
Find and Replace Methods
Finding and replacing text patterns in Vim simplifies navigation and changes in large files.
Hence, Vim offers two ways to search and replace text, depending on whether a single change or multiple edits are necessary.
Warning: Vim must be in normal mode to execute commands. Go back to normal mode with the Esc key.
Slash and Dot Command
Use the slash and dot command to find and replace a single occurrence of a word in Vim.
Open a file in Vim and follow these steps:
- Press the slash key (/).
- Type the search term to highlight it in the text and hit Enter.
- Use the Esc key to return to normal mode.
- Type
cgn
and enter the replacement term.
- Go back to normal mode.
- Hit n to move to the next search term occurrence.
- Press the dot (.) key to replace the next occurrence with the same replacement term.
Note: The dot method also works with other Vim search methods.
Substitute Command
The substitute command :s
is more versatile than the slash and dot command.
The basic :s
command syntax is:
:s/search_term/replacement_term/
The command searches for the search_term
in the current line, the line where the cursor is. Next, :s
changes the search_term
to the replacement_term
.
For example, to find the term vi in the current line and replace it with Vim, use the following command:
:s/vi/Vim/
Hit Enter and the terminal shows that the string vi changed to Vim:
However, :s
offers much more than the basic search and replace function. For instance, the command accepts different options and flags. The complete syntax for the substitute command looks like this:
:[range]s/search_term/replace_term/[flags] [count]
These options and flags help specify the search. For example, to find all occurrences of vi in the current line, add the g
flag.
:s/vi/Vim/g
Hit Enter to replace both instances with Vim:
Moreover, confirm each substitution with the c
flag:
:s/vi/Vim/gc
Output is the following:
Vim prompts users to choose between several options:
- Substitute selected match with Y.
- Skip selected match with n.
- Replace all matches with a.
- Quit the command with q.
- Replace the selected match and quit with I.
However, the ^E and ^Y options are available in some Vim installations. Use them to scroll the screen:
- Scroll the screen up with Ctrl + E.
- Scroll the screen down with Ctrl + Y.
Find and Replace Important Considerations
The find and replace tools in Vim, especially the substitute command, offer additional possibilities for a more specific and effective search. Check out how to customize the search and replace operation in the following text.
Search Range
Specifying a range when running the :s
command helps users search and replace patterns beyond the current line.
To determine the range:
- Add line numbers separated by a comma to find and replace a pattern in specific lines.
For example, substitute all occurrences of vi to Vim from line 1 to line 3:
:1,3s/vi/Vim/g
The output shows that only the first two instances of vi, in the first three lines, were changed to Vim:
- Choose the
%
symbol and add theg
flag to find and replace a pattern in the entire file.
:%s/vi/Vim/g
- Use a dot (
.
) and a dollar sign ($
) separated by a comma to substitute everything from the current line to the last line.
:.,$s/vi/Vim/g
Since the current line is also the last line, the output shows that the instances of vi in the last line were changed.
- Combine
+
/-
symbols with numbers to add or subtract lines from the current one.
For example, to substitute each vi with Vim starting from the current line and the next two lines, type:
:.,+2s/vi/Vim/g
Note: Learn how to show line numbers in Vim with this guide.
Case Sensitivity
The find and replace process in Vim is case-sensitive by default. For example, using the :s
command to substitute vim (in lowercase) with vi does not work. This is because Vim is always in capitalized in this text. Therefore, use Vim instead of vi as the search term.
To make the find and replace operation case insensitive, use one of the two methods:
- Add the
i
flag.
:%s/vim/vi/gi
The output shows that both instances of Vim have been found and replaced with vi.
Use the I
flag to turn the case-sensitive search back on.
- Append
\c
after the search pattern.
:%s/vim\c /vi/g
The output shows that even though the search term is lower case, the command finds the uppercase Vim in the text.
Adding the /C
after a search pattern turns on case-sensitive search.
Word Substitution
The :s
command, by default, searches for patterns, not unique words. For example, using vi as the search pattern results in matches where vi is part of a larger word, such as visible.
Executing the command in the entire text changes patterns included in other words as well.
Search and replace a whole word with:
:%s/\<search_term\>/replace_term/
The \<
symbol marks the beginning and the \>
symbol the end of a word. For example, to search for the word vi, but not when part of a bigger word, and replace the word with Vim, use:
:%s/\<vi\>/Vim/g
Find and Replace History
Running the :s
command without any options shows previous substitute operations. In addition, run the :s
command and use the up/down arrow keys to move across the document.
More importantly, use the :s
command to apply previous substitutes to new text. For example, the history shows that the command replaced all instances of vi with Vim.
Running :s
finds any already made substitutes, but also all instances of vi not changed yet.
Find and Replace Examples
The find and replace function in Vim has extensive practical use. Thus, outlined below are common substitute command examples.
Find and Replace Entire Lines
Use :s
to substitute entire lines in Vim based on a single parameter. For example, substitute every line starting with Do you with Learn more about Vim! using this command:
:%s/^Do you .*/ Learn more about Vim!/g
The ^
(caret) symbol matches the beginning of a line, and *
matches the rest of the line.
The output shows the change:
Change Number in Numbered List
Adding an item in the middle of a numbered list in Vim means that the user has to change numbers in all the subsequent lines.
The substitute command changes the numbers of all other articles accordingly. Use this command:
:5,$s/\d\+/\=submatch(0) + 1/
The command consists of:
:5,$
- The range from the fifth line to the last line.\d\+
- The digits sequence as the search term.\=submatch(0) + 1
- The replacement term adding 1 to each match.
After executing the substitute command, the output looks like this:
Eliminate Double Spaces
Double spaces are common typing errors, but :s
takes care of this quite quickly with:
:%s/[double space]/[single space]/g
All spaces in the last line are doubled. After executing the command, the sentence is single-spaced:
Replace Different Words with One
Use the substitute command to substitute different search terms with one replacement term. For example, replace every instance of vi and Vim with This text editor.
:%s/\(vi\|Vim\)/This text editor/g
The changed text looks like this:
Delete All Pattern Occurrences
Instead of substituting the search term, delete the pattern by omitting the replacement term. For example, delete all instances of Vim with:
:%s/Vim/
Conclusion
After reading this tutorial, you know how to find and replace text in Vim.
Next, check out and download this useful Vim Commands Cheat Sheet to learn other common Vim commands.