Vim - Find and Replace

July 28, 2022

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.

Vi/Vim - Find and Replace

Note: If you don't have Vim, check out these tutorials and learn how to install the text editor on CentOS or Ubuntu.

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:

  1. Press the slash key (/).
  2. Type the search term to highlight it in the text and hit Enter.
Vim the Slash and Dot Command Using the Slash Key Terminal Output
  1. Use the Esc key to return to normal mode.
  2. Type cgn and enter the replacement term.
Vim the Slash and Dot Command With the Replacement Term Terminal Output
  1. Go back to normal mode.
  2. Hit n to move to the next search term occurrence.
Vim the Slash and Dot Command - Using n Terminal Output
  1. Press the dot (.) key to replace the next occurrence with the same replacement term.
Vim the Slash and Dot Command - Dot Terminal Output

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/
Vim the Substitute Command Finding a Term Terminal Output

Hit Enter and the terminal shows that the string vi changed to Vim:

Vim the Substitute Command Replacing a Term Terminal Output

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
Vim the Substitute Command With g Flag Process Terminal Output

Hit Enter to replace both instances with Vim:

Vim the Substitute Command With g Flag Result Terminal Output

Moreover, confirm each substitution with the c flag:

:s/vi/Vim/gc
Vim the Substitute Command with c flag Process Terminal Output

Output is the following:

Vim the Substitute Command With c Flag Options Terminal Output

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:

  1. 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
Vim the Substitute Command With Line Numbers Separated by a Comma Process Terminal Output

The output shows that only the first two instances of vi, in the first three lines, were changed to Vim:

Vim the Substitute Command With Line Numbers Separated by a Comma Terminal Output
  1. Choose the % symbol and add the g flag to find and replace a pattern in the entire file.
:%s/vi/Vim/g
Vim the Substitute Command With % and g Flag Terminal Output
  1. 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
Vim Substitute Command With the Dot and Dollar Sign Process

Since the current line is also the last line, the output shows that the instances of vi in the last line were changed.

Vim Substitute Command With the Dot and Dollar Sign Command Terminal Output
  1. 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
Vim the Substitute Command-Using + and- Symbols With Numbers

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:

  1. Add the i flag.
:%s/vim/vi/gi
Vim Substitute Command With the i Tag Terminal Output

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.

  1. Append \c after the search pattern.
:%s/vim\c /vi/g
Vim the Substitute Command With the Appended c Terminal Output

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.

Vim the Substitute Command Searching for Whole Words Terminal Output

Executing the command in the entire text changes patterns included in other words as well.

Vim Substitute Command Replacing Words Nested in Other Words Terminal Output

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
Vim the Substitute Command Find and Replace Whole Word Terminal Output

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.

Vim Substitute Command History Terminal Output

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
Vim Substitute Command Replacing Entire Line Terminal Output

The ^(caret) symbol matches the beginning of a line, and * matches the rest of the line.

The output shows the change:

Vim Substitute Command Replacing Entire Line

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.

Vim Adding an Item to a Numbered List

The substitute command changes the numbers of all other articles accordingly. Use this command:

:5,$s/\d\+/\=submatch(0) + 1/
Vim Substitute Command Changing Numbers in a Numbered List

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:

Vim Substitute Command Changed Numbers in a Numbered List

Eliminate Double Spaces

Double spaces are common typing errors, but :s takes care of this quite quickly with:

:%s/[double space]/[single space]/g
Vim Substitute Command Finding Double Space Terminal Output

All spaces in the last line are doubled. After executing the command, the sentence is single-spaced:

Vim Substitute Command Eliminating Double Spaces Terminal Output

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
Vim the Substitute Command Replacing Different Words With One Process

The changed text looks like this:

Vim the Substitute Command Replacing Different Words With One Result Terminal Output

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/
Vim Substitute Command Deleting Words Terminal Output

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.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
Vim - Go to End of File
July 27, 2022

Vim is equipped with functional tools for navigating big files, including jumping to the top or bottom of the text. This guide will show you how to go to the end or top of a file in Vim and enable you to edit different sections...
Read more
How to Exit (Quit) Linux Vim/Vi editor
June 17, 2020

Vim text editor is one of the most widely used text editor. You should learn some of the basic commands used in Vim. This article shows you how to exit a file...
Read more
How to Undo and Redo Changes in Vim / Vi
April 28, 2020

Mastering Vim commands includes learning how to undo and redo changes. Knowing how to undo and redo in Vim will help you speed up and simplify...
Read more
How to Change and Use Vim Color Schemes
March 9, 2020

Vim color schemes are a handy feature for setting syntax highlighting. You can choose from a variety of ready-made themes or configure your own color set...
Read more
  • © 2022 Copyright phoenixNAP | Global IT Services. All Rights Reserved.