Vim – Find and Replace – phoenixNAP

introduction

Manipulating text and

making multiple edits at once is crucial when working in Vim or its previous 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 feature in Vim/Vi and track all changes.

Find

and replace methods Finding and replacing text patterns in Vim

simplifies navigation and changes to large files

. Therefore, Vim offers two ways to find and replace

text, depending on whether a single change or multiple edits are necessary

.

Forward slash

/period

command Use the forward slash and period command to find and replace a single occurrence of a word in Vim. Open a file in Vim and follow these steps: Press the forward slash (/) key. Type the search term to highlight it in the text and press Enter. Use the Esc key to return to normal mode. Type cgn and

  1. enter the replacement term
  2. . Return

  3. to normal mode
  1. .
  1. Press n to move to the next occurrence of the search term.
  1. Press the dot (.) key to replace the next occurrence with the same replacement term.

Surrogate command

The substitute :s command is more versatile than the forward slash, period command. The

basic syntax of

the :s command is:

:s

/search_term/replacement_term/

The command searches for the search_term on the current line, the line where the cursor is. Then, :s changes the search_term to replacement_term.

For example, to find the term vi on the current line and replace it with Vim, use the following command: :s/vi/Vim/ Press Enter and the terminal will show that the string vi changed to

Vim

:

However, :s offers much more than the basic search and replace function. For example, the command accepts different options and flags. The complete syntax for the surrogate 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 on the current line, add the indicator g.

:s/vi/Vim/g

Press Enter to replace both instances with Vim: Also, confirm each substitution with the flag c: :s/vi/Vim/gc The output is as follows: Vim prompts users to choose from several options: Replace the selected match with Y. Ignore the selected match with n. Replace all matches with

  • a .
  • Exit the command with q.
  • Replace the selected match and exit with I.

However, ^E and ^Y options are available in some Vim installations. Use them to scroll

the screen: Scroll up with Ctrl +

    E.

  • Scroll down the screen with Ctrl + Y

. Find and replace

important considerations

The search and

replace tools in Vim, especially the substitute command, offer additional possibilities for a more targeted and effective search. See how to customize the find and replace operation in the following text.

Search

range

Specifying a range when running the :s command helps users find 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 on specific lines.

For example, substitute all occurrences of vi to Vim from line 1 to line 3: :1,3s/vi/vim/g The result shows that only the first two instances of vi, in the first three lines, were changed

to Vim: Choose the % symbol and add the

  1. g flag to find and replace a pattern throughout the file.:%

s/vi/Vim/g Use a

  1. period (.) and a dollar sign ($) separated by a comma to replace everything, from

the current line to the last line.:.,$s/vi/Vim/g Because the current line is also the last line

, the output shows that the vi instances on the last line were changed

.

  1. Combine +/- symbols with numbers to add or subtract lines from the current one.

For example, to replace each vi with Vim from the current line and the next two lines, type: :.,+2s/vi/Vim/g Case sensitivity

The find and

replace process in Vim is case-sensitive by default. For example, using the :s command to replace vim (lowercase) with vi does not work. This is because Vim is always capitalized in this text. Therefore, use Vim instead of vi as your search term.

To make the find and replace operation case-insensitive, use one of two methods:

  1. Add the flag i

.:%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 case-sensitive search back

on.

  1. Append c after the search pattern.

:

%s/vimc /vi/g The

result shows that even if the search term is lowercase, the command finds the uppercase Vim in the text

.

Adding the /C after a search pattern activates case-sensitive search.

Word substitution

The :s command , by default, looks for patterns, not unique words. For example, using vi as a search pattern results in matches where vi is part of a larger word, such as visible.

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

Find and replace an entire 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 it is part of a larger word, and replace the word with Vim, use: :%s/<vi>/Vim/

g

Find and replace history

When you run the :s command without any options, the above substitution operations are displayed. Also, run the :s command and use the up/down arrow keys to move through the document.

More importantly, use the :s command to apply older substitutes to new text. For example, the history shows that the command replaced all instances of vi with Vim.

Running :s finds any ready-made substitutes, but also all instances of vi that have not yet changed.

Find

and replace examples

The find and replace function in Vim has a wide practical use. Therefore, common examples of substitute commands are described below.

Find and

replace entire lines

Use :s to replace entire lines in Vim based on a single parameter. For example, replace each line that begins with Do you with Learn more about Vim! using this command: :%s/^Do you .*/ Learn more about Vim!

/g The

symbol ^(caret) coincides with the beginning of a line and * coincides with 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 the numbers on all subsequent lines

.

The substitute command changes the numbers of all other items 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

  • sequence of digits as a search
  • term.

  • =submatch(0) + 1 – The replacement term that adds 1 to each match.

After running the substitute command, the output looks like this

:

Remove

double spaces Double spaces are

common typing errors, but :s takes care of this pretty quickly with: :%s

/[double space]/[single space]/g

All spaces on the last line are duplicated. After you run the command, the sentence has only one space:

Replace different words

with a

Use the substitute command to replace different search terms with a replacement term. For example, replace each instance of vi and Vim with This text editor.

:%s/(vi| Vim)/This text editor/g

The modified text looks like this:

Remove all pattern occurrences

Instead of replacing the

search term, remove 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.

Then take a look and download this handy Vim Commands cheat sheet to learn other common Vim commands.

Contact US