Global search and replace using Vi

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.


Comments

3 responses to “Global search and replace using Vi”

  1. 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

  2. *shrug* I was simply illustrating the task that I was doing at that moment.

  3. 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.