Vim, Vi Search Find And Replace Texts

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/vim-vi-search-find-and-replace-texts/
PINTEREST
PINTEREST
INSTAGRAM

Vim is a powerful tool for text editing code developments etc. One of the most wanted and loved feature is find and replace simple substitute. We will look different ways to find and replace operation in vim. Our text file named file1 is like below.

Simply Replace with Vim

To work with replace function we need to open text file with vim and then run replace command.

Simply Replace
  • %s will look all lines for /line occurrence
  • /LINE will be replaced with /g flag.

Replace In Single Line

If we want to replace only in single line not in the whole file we will remove % from command like below. We will change LINE to the line

Replace In Single Line

Ask For Confirmation

We can confirm the changes one by one while changing them with the command below.

Ask For Confirmation

Replace Only Whole Words

To prevent errors like not word change we can specify to change only the whole words.

Replace Only Whole Words

We have only changed the word LINE which is surrounded with spaces.

Specify Replace Range

We can specify search range by providing line numbers. Substitution will occur only in the range of this numbers.

Specify Replace Range

There will be no substitution because we have specified lines between 2 and 3 where there is no search result for LINE

Search Incasesentive

We can search incasesensitively and then replace with the following command.

Search Incasesentive
Search Incasesentive
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/vim-vi-search-find-and-replace-texts/
PINTEREST
PINTEREST
INSTAGRAM

Leave a Reply

Your email address will not be published.