Pattern matching in vi Editor

To learn about pattern matching, we will ensure that the pattern that we will search for is highlighted when the pattern searched for is found. The configuration file for vi is /etc/vimrc.In the vi editor, use the following commands for various options:





:set hlsearch

Highlights the search pattern


:se[t] showmode

Shows when you are in insert mode


:se[t] ic

Ignores case when searching


:set noic

Shows a case-sensitive search

The user should open the file in vi, press the Esc button so that it enters command mode, and then type colon, followed by these commands. The following are the commands for pattern search and replace:






This searches for the pattern pat and places the cursor where the pattern occurs



This repeats the last search



Globally, all the occurrences of old will be replaced by new



#,# should be replaced with the numbers of the two lines (say between line numbers 3 and 6), for example, 3,6s/am/was/g

The following is an example of a regular expression for replacing Tom with David:

:1,$s/tom/David/g                // from line 1 to end ($), replace tom by David 
:1,$s/<[tT]om>/David/g   // start and end of word   <    > 

This is another example of a regular expression. Create the love.txt file, as follows:


Man has love for Art
World is full of love
Love makes world wonderful
love looove lve
Love love lover loves
I like "Unix" more than DOS
I love "Unix"/
I said I love "Unix"
I love "unix" a lot

Use the following commands for testing pattern-searching facilities:



:set hlsearch

This will highlight the search pattern, when it is found


This will highlight any text matching with love. Use n for forward and N for backward in the next search


This will highlight the line starting with love


This will highlight the line ending with love


This will highlight a line containing only the word love


This will highlight any character match for .


This will highlight love, loooove, and lve


This will search for patterns Love and love


This will highlight any matching character in the a to z range

/ove[^a-zA-Z0-9" "]/

Except for alpha-numeric characters, this will match punctuation marks such as , ; : and similar


This will replace unix with Linux


This will replace unix with Linux from line 1 to the end ($)


This will start and end of word < >


This will highlight a line starting with an uppercase letter, two chars and then end of line

/^[A-Z][a-z ]*3[0-5]/

This will highlight any line ending with 30 to 35

/[a-z]* ./

This will highlight any line with lowercase and ending with .

Related Articles

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

read more

Lorem ipsum dolor sit amet consectetur


Submit a Comment

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

fourteen − five =