Vi and Vim Search and Replace Operations


Vim provides reach features for text and word search and replace operations. Sed and awk provides similar features to Vim but vim is text editor which makes it different from others.

Sample Text

During examples we will use following sample text.

Search and Replace In Whole Document

The most popular use  case is search and replace in whole document. We will use search commands. In this example we will search with %s for database and change to DB in whole file with .

Search and Replace In Current Line

We can shrink the scope to the current line where cursor locates. We will remove % from s search parameter.

Ask Before Replace

Another useful feature of search and replace is asking or confirming before replace operation. We will add c after g option like below.

LEARN MORE  Vim, Vi Search Find And Replace Texts

Search Only Whole Words and Replace

By default search and replace operation is done without looking whole word. Event is searched text is a part of word it will be replaced. We can only search in whole words by surrounding the search text with \< and \> like below. We will search base as a whole word.

Search Case Insensitive and Replace

Vim search mechanism search case sensitive by default. We can turn of case insensitivity. We will provide i after g option.

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)