Monday, April 23, 2012
3:16 PM

Using the substitute command in VI editor

In this post we will see the various ways in which we can search and replace strings in the "vi" editor.

To search and replace in the vi editor we have to use the command "substitute" or the shorter option "s".

Please note that all the substitute commands give below have to run in the command mode of the editor.

We will use this test file for all the examples :



The syntax of the command is

[range]s/{search string}/{replace string}/[flags] [count]

range : specifies the lines in which the replacement has to be carried out, if left blank the current line is considered.
flags: Controls various othe features of the substitue,we will see a few options.
count: The count number of replacements are carried out in the file

Let use some examples to understand the working

Simple substitute in the current line :

Replacing only one occurence :

To replace a string only in the line the cursor is present at we can use

s/{search string}/{replace string}/

If we want to replace the word "is" with "was" in the first line of the example file then move the cursor to first line and execute the command :



As a result of this the first occurance of "is" in the line will be replaced by "was".

Replacing all the occurences:

If we want the same command to be applied to all the matching stings in the current line we will have to use the flag "g", which means global.



To be prompted before each replace:

If we want to be prompted for confirmation before replacing the string, we can use the flag "c".

The matched string will be highlighted an will be presented with the options



e.g.



To replace in the whole file:

To apply the same command to all the lines inthe file we will have to specify the range. The range is given in line numbers, so to specify the whole file we can use the range 1,$ which indicates starting from the first line till the end of the file, which is indicated by the symbol $ e.g.



Restricting to specific lines

The range can also be restricted to a specific line numbers by using the numbersin the range filed.
For eg if we want to replace the word linux with Linux in lines 2,3 and 4 we can pass the range 2,4 i.e.



Note: we can use the command :set number to find out the line numbers.

In built options:

The substitute commands also has few useful options for the search strings.
For e.g. the option "\u" converts the next character to upper case.



The above command searches for the string "linux".
The \0 points to the whole matched pattern i.e linux in this case.
Thus we are replacing linux with linux, but we have also added the \u option before \0, thus the next character after "\u" i.e. "l" in this case gets converted to upper case converting the string from "linux" to "Linux".

To convert first characters of all the lines to upper case :



To convert first letter of every word to upper case :



To look into more options of substitute look at the help in vi


0 comments:

Post a Comment