Re: g/pat/s/pat.*/to/ vs :%s/pat.*/to/

2006-11-22 Thread Mikolaj Machowski
On śro lis 22 2006, Vim List wrote:
> In vim docs, there is example like this:
>g/pat/s/pat.*/to/
> As far as I know, it is also possible to do it without g:
>%s/pat.*/to/
> Is there a difference, a reason to prefer the
> first form, the longer for with 'g' ?

It is useful when you want to change some pattern but only in lines
where is other pattern. Example::

g/^#IDO/s/\\/\//g

It will change \ in / but only in lines beginning with '#IDO'. 
Combining of g// and s/// with similar {from} string may be faster to
execute (not type ;) when s/// is very complex regexp. You prefilter
file to catch only lines where possibility of substitution is high and
later you only test this subset of lines. But it pays off only on big
files and complicated regexps.

m.




Re: g/pat/s/pat.*/to/ vs :%s/pat.*/to/

2006-11-22 Thread Tim Chase

In vim docs, there is example like this:
   g/pat/s/pat.*/to/
As far as I know, it is also possible to do it without g:
   %s/pat.*/to/
Is there a difference, a reason to prefer the
first form, the longer for with 'g' ?



I don't think in the example you gave, that there's a significant 
difference, if any.


However, where the pattern (so to speak) is useful, is where you 
want something like


:g/pat1/s/pat2/replacement

where rather than using the same pattern, you have two different 
patterns.  I use this frequently to do things like


:g/echo/s/^/#

to comment out any lines in a script that use the "echo" command. 
 Yes, this could be rewritten as


:%s/.*echo/#&

but the former makes clearer sense in my head.

It's particularly useful if you have pieces that you're searching 
for that overlap:


text:   aaabbbcccddd
g/b.*d/s/a.*c/XXX

where the pattern matches one fragment, and the s// matches an 
overlapping, but not-quite-the-same-end-points fragment of the line.


-tim





g/pat/s/pat.*/to/ vs :%s/pat.*/to/

2006-11-22 Thread Yakov Lerner

In vim docs, there is example like this:
  g/pat/s/pat.*/to/
As far as I know, it is also possible to do it without g:
  %s/pat.*/to/
Is there a difference, a reason to prefer the
first form, the longer for with 'g' ?
Thanks
Yakov