[comp.unix.questions] A question on .exrc while using vi

joshi@motcid.UUCP (Abhay B. Joshi) (12/04/90)

I couldn't find a newsgroup for vi questions, hence this intrusion.

I have a simple text-file containing the following text (3 lines): 

	The goal:
	All I am trying to do is replace the 'the's by 'an's.
	That's all.

I set up a .exrc in the current directory which looks like (2 lines):
	:g/the/ s//an/g

Now I tried to do a 'vi text-file', I got a coredump! About 10 times.
Then I deleted the .exrc file and put the command in EXINIT.
I got a coredump again. (No coredump if neither .exrc nor EXINIT exists)

I don't think it's a memory problem. I use a SUN 4.0.3 with sunview with lots
of programs running at the same time. I tried the above in a no-other-program-
running mode too.

Any clues to the solution would be very welcome.

Abhay

jeenglis@alcor.usc.edu (Joe English Muffin) (12/04/90)

joshi@motcid.UUCP (Abhay B. Joshi) writes:
>
>I set up a .exrc in the current directory which looks like (2 lines):
>	:g/the/ s//an/g
>
>Now I tried to do a 'vi text-file', I got a coredump! About 10 times.
>Then I deleted the .exrc file and put the command in EXINIT.
>I got a coredump again. (No coredump if neither .exrc nor EXINIT exists)

Just a guess:  I think that vi executes all the commands
in .exrc BEFORE reading the file to be edited.  (Try leaving
blank lines in .exrc -- you'll get messages like "No lines
in the buffer" even when you specify a filename on the
command line.)

I'm not sure what your actual application is, but you're better
off mapping a macro to the substitute command.

More weirdness I just discovered: if there are blank lines in 
.exrc, 'vi .exrc' hangs completely.  This is under SunOS 4.1
on a Sun-3.


--Joe English

  jeenglis@alcor.usc.edu

tchrist@convex.COM (Tom Christiansen) (12/04/90)

In article <5315@graphite20.UUCP> joshi@motcid.UUCP (Abhay B. Joshi) writes:
:I couldn't find a newsgroup for vi questions, hence this intrusion.

Well, there is comp.editors, but you didn't know that.  

:I have a simple text-file containing the following text (3 lines): 
:
:	The goal:
:	All I am trying to do is replace the 'the's by 'an's.
:	That's all.
:
:I set up a .exrc in the current directory which looks like (2 lines):
:	:g/the/ s//an/g

What a funny place to put that command.  Plus you don't use colons
in your .exrc file.

:Now I tried to do a 'vi text-file', I got a coredump! About 10 times.

You know, one of the indications of insanity is doing the same
thing again and again and expecting different results. :-)

:Then I deleted the .exrc file and put the command in EXINIT.
:I got a coredump again. (No coredump if neither .exrc nor EXINIT exists)

:I don't think it's a memory problem. I use a SUN 4.0.3 with sunview with lots
:of programs running at the same time. I tried the above in a no-other-program-
:running mode too.

Um, that's kind of a peculiar way to do about it.  How come
you're putting it in your .exrc?  If this is something you want
to do automatically to a file, you could run sed or ex over it:

    sed 's/the/an/g' < file1 > file2 && mv file2 file1

or else

    ex - file <<EOEDIT
    %s/the/an/g
    x
    EOEDIT

Of course, you probably really want \<the\> instead so "bathe"
doesn't become "baan".  (That only works with ex though.)

Oh, and this should do the trick, too 

    perl -p -i -e 's/\bthe\b/an/g' file

The \b in perl is \< or \> in ex.  If you'd like the old 
file around as file.bak, then do this:

    perl -p -i.bak -e 's/\bthe\b/an/g' file

If, however, this is something you are expecting to do 
all the time when you're in the editor, you could make 
a macro to do it for you.  Put this in your .exrc, and 
don't put any blank lines there.

    map = :%s/\<the\>/an/g^M

Make your to put a real ^M in the file.  Now you just 
have to hit = to make the change.


--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
"With a kernel dive, all things are possible, but it sure makes it hard
 to look at yourself in the mirror the next morning."  -me

mju@mudos.ann-arbor.mi.us (Marc Unangst) (12/05/90)

joshi@motcid.UUCP (Abhay B. Joshi) writes:
> I have a simple text-file containing the following text (3 lines): 
> 
> 	The goal:
> 	All I am trying to do is replace the 'the's by 'an's.
> 	That's all.

Son, vi is the wrong tool for the job.

ed file <<Frobozz
1,$g/the/s//an/g
w
q
Frobozz

or

sed 's/the/an/g' file >file.new

(You could use awk to do it, too, but awk is really the wrong tool for
the job here.  The nice thing about Unix is that it has so many
different tools to choose from.)

--
Marc Unangst               |
mju@mudos.ann-arbor.mi.us  | "Bus error: passengers dumped"
...!umich!leebai!mudos!mju | 

james@dlss2.UUCP (James Cummings) (12/06/90)

In article <q5LoT4w163w@mudos.ann-arbor.mi.us> mju@mudos.ann-arbor.mi.us (Marc Unangst) writes:
>joshi@motcid.UUCP (Abhay B. Joshi) writes:
>> I have a simple text-file containing the following text (3 lines): 
>> 
>> 	The goal:
>> 	All I am trying to do is replace the 'the's by 'an's.
>> 	That's all.
>
>Son, vi is the wrong tool for the job.
>
>ed file <<Frobozz
>1,$g/the/s//an/g
>w
>q
>Frobozz
>
	Well, I wouldn't say vi is the "wrong tool for the job"!  Simply
do the following:

	:%s/the/an/g

	This is the equivalent of the above ed command...which boils down
to a matter of personal taste.
	The only problem I see with either of these solutions, is that the 
word "theatre" becomes the word "anatre"....boo-hisss!  A more practical 
solution would be in three steps, like so:

	:%s/ the / an /g
	:%s/^the /an /g
	:%s/ the$/ an/g

	There is probably a fancier way to do all of this in one swift move
but it escapes me for now.  Do note the use of spaces and the fact that we
are also looking for the 'the's that are at the begining and ending of lines
while preserving the integerity of words like "either", "theatre", etc.

stadt@cs.utwente.nl (Richard van de Stadt) (12/06/90)

In article <5315@graphite20.UUCP>, joshi@motcid.UUCP (Abhay B. Joshi) writes:
|> I couldn't find a newsgroup for vi questions, hence this intrusion.
|> 
|> I have a simple text-file containing the following text (3 lines): 
|> 
|> 	The goal:
|> 	All I am trying to do is replace the 'the's by 'an's.
|> 	That's all.
|> 
|> I set up a .exrc in the current directory which looks like (2 lines):
|> 	:g/the/ s//an/g
|> 
|> Now I tried to do a 'vi text-file', I got a coredump! About 10 times.
|> Then I deleted the .exrc file and put the command in EXINIT.
|> I got a coredump again. (No coredump if neither .exrc nor EXINIT exists)
|> 

On a SPARCstation 1+ I get a segmentation fault

--
R.R. van de Stadt (Richard)
Email: stadt@cs.utwente.nl

rob@b15.INGR.COM (Rob Lemley) (12/08/90)

In <5315@graphite20.UUCP> joshi@motcid.UUCP (Abhay B. Joshi) writes:

>I couldn't find a newsgroup for vi questions, hence this intrusion.

Try comp.editors, very knowledgeable vi gurus read that group.

skwu@boulder.Colorado.EDU (WU SHI-KUEI) (12/09/90)

In article <q5LoT4w163w@mudos.ann-arbor.mi.us> mju@mudos.ann-arbor.mi.us (Marc Unangst) writes:
..other stuff

>> 	The goal:
>> 	All I am trying to do is replace the 'the's by 'an's.
>> 	That's all.
>
>Son, vi is the wrong tool for the job.
>
>ed file <<Frobozz
>1,$g/the/s//an/g
>w
>q
>Frobozz

... more stuff deleted

>--
>Marc Unangst               |
>mju@mudos.ann-arbor.mi.us  | "Bus error: passengers dumped"
>...!umich!leebai!mudos!mju | 

Sonny boy, it ain't all that easy - your solution changes all instances of
'theatre' to 'anatre', 'gather' to 'gaanr', etc., etc.  It takes three commands
in 'ed' or 'sed' to accomplish the original task:

	g/^the /s//an/g
	g/ the /s//an/g
	g/ the$/s//an/g

If the 'the's may be preceded or followed by tabs as well as spaces, things
may be even more complicated.

tchrist@convex.COM (Tom Christiansen) (12/09/90)

In article <1990Dec9.033641.9731@csn.org> skwu@spot.Colorado.EDU (WU SHI-KUEI) writes:
:>> 	The goal:
:>> 	All I am trying to do is replace the 'the's by 'an's.
:>> 	That's all.
:Sonny boy, it ain't all that easy - your solution changes all instances of
:'theatre' to 'anatre', 'gather' to 'gaanr', etc., etc.  It takes three commands
:in 'ed' or 'sed' to accomplish the original task:
:
:	g/^the /s//an/g
:	g/ the /s//an/g
:	g/ the$/s//an/g
:
:If the 'the's may be preceded or followed by tabs as well as spaces, things
:may be even more complicated.

*PLEASE* don't spread misinformation.  All it takes is a simple \<the\> in
the expression to take care of those cases.

    %s/\<the\>/an/g

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
"With a kernel dive, all things are possible, but it sure makes it hard
 to look at yourself in the mirror the next morning."  -me

rouben@math13.math.umbc.edu (Rouben Rostamian) (12/10/90)

A long, long time ago someone wanted help with:
>>> 	All I am trying to do is replace the 'the's by 'an's.
>>> 	That's all.

There have been several cycles of wrong or partially wrong solutions
followed by the right solution.  I am sure that the original poster
has lost interest in these followups, however since it appears that
we are at the beginning of a new cycle of Rube Goldberg-ish solutions,
I feel that I should remind those who have not been paying attention
that the correct solution is:
:%s/\<the\>/an/g

The regular expression \<the\> picks up the pattern "the" delimited
by whitespace or "the" at the beginning or end of lines.  Therefore
"theatre" does not change into "anatre".

Let's see if this ends the debate.

--
Rouben Rostamian                            Telephone: (301) 455-2458
Department of Mathematics and Statistics    e-mail:
University of Maryland Baltimore County     bitnet: rostamian@umbc
Baltimore, MD 21228,  U.S.A.                internet: rostamian@umbc3.umbc.edu

mercer@npdiss1.StPaul.NCR.COM (Dan Mercer) (12/11/90)

In article <1990Dec6.135646@cs.utwente.nl> stadt@cs.utwente.nl (Richard van de Stadt) writes:
:In article <5315@graphite20.UUCP>, joshi@motcid.UUCP (Abhay B. Joshi) writes:
:|> I couldn't find a newsgroup for vi questions, hence this intrusion.
:|> 
:|> I have a simple text-file containing the following text (3 lines): 
:|> 
:|> 	The goal:
:|> 	All I am trying to do is replace the 'the's by 'an's.
:|> 	That's all.
:|> 
:|> I set up a .exrc in the current directory which looks like (2 lines):
:|> 	:g/the/ s//an/g
:|> 
:|> Now I tried to do a 'vi text-file', I got a coredump! About 10 times.
:|> Then I deleted the .exrc file and put the command in EXINIT.
:|> I got a coredump again. (No coredump if neither .exrc nor EXINIT exists)
:|> 
:
:On a SPARCstation 1+ I get a segmentation fault
:
I won't ask why you're attempting this.  However,  at the time the
exinit processing occurs,  there is not yet an identifiable file (I
ran into this problem several years ago).  
So it is not at all surprising that attempts to edit non-existent data
result in memory errors.  On NCR Towers,  you get "No lines in
buffer".  (On the other hand,  Towers will allow you to vi
/dev/ether0,  which craps out the entire machine).

-- 
Dan Mercer
NCR Network Products Division      -        Network Integration Services
Reply-To: mercer@npdiss1.StPaul.NCR.COM (Dan Mercer)
"MAN - the only one word oxymoron in the English Language"