• Welcome to Valhalla Legends Archive.
 

Find/Replace in vi

Started by iago, February 21, 2004, 07:07 PM

Previous topic - Next topic

iago

I've never been able to figure this out.  In vi, how do you do global substitions across a file?  :s/a/b/g does a substitution across the line, but not the file.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Thing

sed would probably be better to accomplish that task.
That sucking sound you hear is my bandwidth.

Kp

Specify the range of lines you want affected.  If you don't, it assumes current line only.  Some examples:

:%s/foo/bar/  -- substitute across the entire file
:.,$s/foo/bar/  -- substitute on this line and all lines through to the end of the file
:10,.s/foo/bar  -- from line 10 to the current line (dot).
:'a,'bs/foo/bar/  -- from mark a to mark b

You can also do arithmetic expressions.  I often find myself doing :.,.+10s/.../---/ to get this line and the ten following it.  Arithmetic should work on any of the above expressions (though it doesn't make much sense to add to $, which is end of file anyway).  You could do $-10 to get until 10 lines shy of end-of-file.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

iago

Ah, it was the '%' that I needed.  I used to know that, but I had forgotten.

Thanks! :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*