TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [TCLUG:9061] regex



>
> Here's another question. I'm a bit confused by the /g aspect of regex
> replacing in vi. Supposedly, it stands for "global" right? But it only
> does the replace until the end of the line. Then if I do %&g, it will,
> according to "Unix in a Nutshell", "Repeat the last change globally",
> which DOES replace all over the file. Is this just a vi quirk, or what?
>

You can repeat the change filewide by using the command

:1,$ s/foo/bar/g

That "1,$" bit is a range of lines ($ means end-of-file).  You can also use
"." (current line) or any line number.  This works with any ":" command.

So, to do your search & replace only on the first ten lines,
:1,10 s/bar/baz/g

or, to copy the current line through the end of the file to a new file,
:.,$ w newfile.txt

In short, it's not a quirk, it's a feature.