Vi and Vim Search and Replace Operations – POFTUT

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. We will search for database and replace with DB

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.We will search for string database and replace with DB

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.

LEARN MORE  How To Move End of File and Line In Vi or Vim?

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. We will search for database as case insensitive and replace with DB

echo '

Leave a Reply

Your email address will not be published. Required fields are marked *