Pages

Wednesday, November 7, 2007

UNIX - Search and Replace in vi editor

This is about as complicated as it gets in vi, since search and replace sytnax is taken from the UNIX sed (stream editor) command.

>> Global search and replace --> :1,$ s/old/new/g

In english, this means:
From 1 to $ (end of file)
substitute
occurrences of "old"
with occurrences of "new"
globally (i.e., all instances of "old")

I won't go into details on using sed in this section (see the UNIX sections on sed and regular expressions), but you can easily change the range to search and replace from by changing "1" and "$" to your needs.