Stewart Stremler wrote:
begin  quoting Ralph Shumaker as of Wed, Jan 31, 2007 at 12:32:21PM -0800:

I want to know how to use vi to do search and replace. Is there something easier to use than "help" to find out how?

I want to replace all instances of
"^[0-9][A-Z]" with
"^[0-9] [A-Z]".


:%s/^\([0-9]\)\([A-Z]\)/\1 \2/


: -> go to command mode
% -> for every line in the file
s -> substitute
/ -> delimit the parts of the substitution
^ -> start of line
\(    -> start a group, must escape ( with \ because this is a retrofit
[0-9] -> pattern
\)    -> end of the group
\(    -> start a group
[A-Z] -> pattern
\)    -> end of the group
/  -> end the first part
\1 -> contents of first group
\2 -> contents of second group
/  -> end of substitution


I've been making these and other changes in gedit manually. But this is one set that could be done so much faster with regular expressions.


Yup.

Don't know how to get "help" to help, as the vim help is now so huge,
it's hard to find anything unless you pretty much know what you want.


I found the :s/// command in help. But I guess I needed help with regular expressions since that's where I went way wrong. That, and my command lacked the "%".

You assumed "^" is "start of line". It's not. I'm assuming I need to escape that too. "\^" But I'd like to verify each replacement. With the s:/// command in help, it says to use "[c]" after the final "/". (It does not say whether or not the braces are needed. It makes me think that they are needed except that previously in the same command they have "s[ubstitute]/".)

Anyway, you gave me what I needed. I figured that if I try it and something goes amiss, I can just abort without saving the file, then just try again, which is what I did.

I had to make a few adjustments, but it finally worked.
:%s/\^\([0-9]\)\([A-Z]\)/\^\1 \2/c

I appended the c. I escaped the "^". And finally, I inserted "\^" in front of "\1". I went through enough of them to be sure that all was good, and then chose "a" (for "all"). That's 4,226 substitutions down (after having done several hundred by hand).

Is "\1" and "\2" part of regular expressions? Or is that something native to vi?

Is there something simple (like a pamphlet) to explain *just* the basics of regular expressions?

(I just noticed the drift from my subject line which happened before I even sent it. It turns out that I started in "man vi", and very *very* reluctantly started looking in "info vi". But I never had the patience to learn the /language/ (so to speak) of "info". They should consider setting up something like man or info in html.)

Also, in vi, how do I make ":set mouse=a" permanent?


--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-list

Reply via email to