Darren Beale Web developer & early adopter

Global search and replace using Vi

G

to replace all instances of – say – 127.0.0.1 with 10.0.0.1 in a file then using vi you can do:

[code]:%s/127.0.0.1/10.0.0.1/g[/code]

if you mess it up then press u for undo. Of course if it's an important file then back it up first.

<span class="dsq-postid" data-dsqidentifier="265 http://bealers.com/2006/04/28/global-search-and-replace-using-vi/">3 comments</span>

  • Do you think maybe you could have used a simpler example without all of the escapes for the .’s ? how about this? Replace all instances of overcomplicated in a file:

    %s/overcomplicated/simple/g

  • Your example is actually a good one. The fact is that a very common use for global search/replace in vii is to replace ip addresses in .conf files. This example perfectly illustrates doing just that, and is unlikely to confuse anyone with even some experience with posix type os.

By bealers
Darren Beale Web developer & early adopter