Search and replace with VI

To search and replace in VI, you have to use the “substitute” command, which looks like this:

[range]s/[search_string]/[replacement_string]/[options]

Examples

To search and replace all occurrences of OLD with NEW, type:

:%s/OLD/NEW/g

To search and replace all occurrences of OLD with NEW and ask for confirmation with each substitution, type:

:%s/OLD/NEW/gc

To search and replace all occurrences of OLD with NEW between lines 11 and 20, type:

:11,20s/OLD/NEW/g

To search and replace all occurrences of OLD with NEW from the current line to the end of the file, type:

:,$s/OLD/NEW/g

To search and replace all occurrences of OLD with NEW in the current line and the next 10 lines, type:

:,+10s/OLD/NEW/g

To search and replace all occurrences of OLD with NEW from the current line to the first line that contains the string JOUMA, type:

:,/JOUMA/s/OLD/NEW/g

Tags: , , , ,

Add a Comment