From: sgr@alden.UUCP (Stan Ryckman)
Newsgroups: comp.unix.questions
Subject: Re: problem with vi..
Date: 18 Aug 92 17:04:10 GMT
Organization: Alden Electronics, Westboro, MA

In article <2195@fnnews.fnal.gov> shah@fnalb.fnal.gov writes:
|>In article <1992Aug17.155121.21926@njitgw.njit.edu>, cxk6819@hertz.njit.edu (K. Chandrakanth) writes:
|>|> Dear netters,
|>|> 	 I have a sparc workstation 2.
|>|> I mounted the floppy drive and copied few files from the floppy. But
|>|> when I viewed through vi I happened to see ^M at the end of each line.
|>|> When I cat the file it looks normal. Will some one tell me the reason
|>|> for this. If possible please send me a shell script or small c program
|>|> to remove this. I am enclosing a sample of the file.
|>|> 
|>|> 	But when I compile the file it works fine. The control characters
|>|> are really irritating. So help me.
|>|> 
|>|> struct employee {^M
|>|>     e_type typer;^M
|>|>     employee *next;^M
|>|>     char name[20];^M
|>|>     short department;^M
|>|> };^M
|>|> ^M
|>|> struct manager:employee {^M
|>|>         employee* group;^M
|>|>         short level;^M
|>|> };^M
|>|> void print_emp(employee *);^M
|>|> void f(employee *);^M
|>|> void print_emp(employee *e)^M
|>|> {^M
|>|> 
|>|> Please email to me. Thanks in advance...
|>
|>vi the file and type following vi command:
|>
|>:%s/^VM//g

Probably you meant ^V^M.

|>where '^V' is one character <Ctrl>V

I frequently get such files; in fact I prepare Usenet and Email
responses at home on a PC and bring them back on a floppy.
DOS lines end with the carriage return-linefeed (^M^J) sequence,
whereas UNIX just uses ^J.  Hence, vi shows the "extra" ^M.
One might think that "cat" doesn't, since the carriage return
is invisible on the screen when followed by a linefeed (although
"cat file1 > file2; vi file2" would show you that cat DOES actually
pass the ^M characters).

The suggested command (with above fix) will work, but I use it so
often that I put the following into my .login:
    setenv EXINIT "map g G\
    map \m :%s/^V^V^V^M//^M\
    set ignorecase showmatch nu ai shiftwidth=4"
The second line is the relevant one; the rest are just my
preferred default choices.  I don't bother with "g" at the end of
command, since there's never more than one ^M per line.  To enter
this line into your .login using vi, you actually have to type:
map \m :%s/^V^V^V^V^V^V^V^M//^V^M\
i.e., one ^V escape for each control character you want, which
includes ^V characters.

This mess sets up a vi macro which lets you type the two character
sequence "\m" to get rid of all the ^M characters.  Advantage:
once you get it right, you just leave it alone.  Note: normally,
you have to type the "\" and "m" within about a second, or the
"timeout" feature (unless you disable it, which you won't want
to if you use function keys etc.) will assume the "\" was
intended as a standalone command.  You can map it into some unused
single vi character (such as "K"), or a function key, if you prefer
that to "\m".

One other thing: if your files come from DOS originally, you
may find a ^Z at the end.  (That's Bill Gates' idea of a joke,
I think.)  You can kill that too.

Stan.
--
This .signature has expired.  Call 1-900-YOU-FOOL to find out why.
    Stan Ryckman   sgr@alden.UUCP
Newsgroups: comp.unix.questions