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



Reply via email to