Searching and Replacing in vi Editor

In Linux, vi is powerful to search and replace the text. Vi provides the :s (substitute) command for search and replace.

To string a text string

:s/yourtext
To see if there are additional occurrences of the same text string, type n.

Search STRING forward : / text
Search STRING backward: ? text
Repeat search: n
Repeat search in opposite direction: N

To replace one text string with another text string in the current line

:s/old_text/new_text/

To replace each occurrence of the text string:

:%s/old_text/new_text/g

Beside the above normal tips, vi also has other powerful searching/replacing abilities: search ranges, using other delimiters, getting search list etc.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditShare on StumbleUponEmail this to someoneShare on TumblrDigg this

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">