Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > Carraige return vs. ^M puzzlement (in vi)

Carraige return vs. ^M puzzlement (in vi)
Thread Tools
neilw
Mac Enthusiast
Join Date: Jun 2000
Location: New Jersey, USA
Status: Offline
Reply With Quote
Dec 23, 2002, 11:54 AM
 
OK, I've wondered about this for a while, but now it's actually causing me problems, so time to figure it out.

In vi, sometimes text files will show up with the text flowed together, and lines separated by "^M". If I then execute the command

:1,$s/^M/^M/g

(where each "^M" is actually cntl-V cntl-M) then the "^M"s magically disappear and the file looks normal again. Frankly I'm not sure why that command should have any effect at all.

This leaves me baffled as to what is in the file that sometimes makes vi interpret the cntl-M's as random control characters, and other times as proper carraige returns. It must be somthing simple but I'm baffled.

Thanks for any help!
     
Zim
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status: Offline
Reply With Quote
Dec 23, 2002, 02:42 PM
 
Uh, your command has me baffled.. while not a vi-guru, it looks like a substitute ctrl-M with ctrl-M, and how that would make a difference is indeed baffling.

However, from a lot of PC/Unix/Mac emacs experience, if you see

abcd^M
defgh

you can substitute ^M with nothing and that will be just fine.

If you see abcd^Mdefg, then you need to put a ctrl-J in its place (at least that's what I use in emacs)

For programmers I think the unix EOL is \n and Windows uses \r.

Correct me if I'm wrong on this. It doesn't answer the question, but maybe provides a little more info on the problem.

Mike
     
Paul McCann
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Dec 23, 2002, 07:13 PM
 
OK, so I'm willing to join the ranks of the baffled: I started with a file using carriage returns (ie a "classic mac" text file) and performed the ^M => ^M substitution as per the first post. Bingo: not only does vi treat the "^M" as a linefeed, but in fact it *is* a linefeed now. Hmm, s'pose I should demonstrate this: start with a file "mactest" containing something like

netwo^Mthree^Mfour^M^M

Now just check that they're genuine carriage returns so that there's nothing hidden here:

% perl -ne 'print "found CR on $.\n" if /\015/' mactest

found CR on 1

% perl -ne 'print "found LF on $.\n" if /\012/' mactest

(ie, nothing...)

Now going into vi and making the substitution (where ^M=control-v control-M)

:%s/^M/^M/g

(note: % is a quick way of entering 1,$ in case you haven't run across this timesaver) immediately makes the file *appear* to be using linefeeds; that is, the ^M's disappear and the text "returns" visually.

netwo
three
four


Yeah, there's a blank line in there at the end. More to the point they really *are* LF's:

% perl -ne 'print "found LF on $.\n" if /\012/' mactest
found LF on 1
found LF on 2
found LF on 3
found LF on 4

So it looks like there's some sort of translation happening internally. Let's just say that I'm awaiting a white knight to come charging in at this point and show us why this is perfectly obvious!

Cheers,
Paul
     
samthecat
Fresh-Faced Recruit
Join Date: Sep 2002
Location: Sydney, Australia
Status: Offline
Reply With Quote
Dec 24, 2002, 01:08 AM
 
ok, here we go...

unix uses one line-feed char as end of line (\n)

traditional mac uses one carriage-return as end of line (\r)

windows/dos uses one of each (\r\n)


the vi command

:%s/^M/^M/g

will search for ^M (which is a carriage-return -- \r) and replace that with a carriage-return...but, this replacement is as if you pressed the return key, which marks the end of line, which will put a line-feed in the file.

simple, neh?

just fyi, there once was an operating system which put a length byte at the beginning of the line and didn't use an end-of-line marker at all.

cheers,

samthecat
     
neilw  (op)
Mac Enthusiast
Join Date: Jun 2000
Location: New Jersey, USA
Status: Offline
Reply With Quote
Dec 26, 2002, 11:27 AM
 
I'll accept that explanation but it's still a little strange. I'm not sure why vi treats the ^M in the second part of the substitution as a keypress rather than as the actual character.

This little exploration stemmed from the fact that Mastercook (ancient version, running in Classic) will only import properly if the recipe file uses carraige returns, so I need to do the reverse translation when using most files saved by OSX which use newlines. So now I'm just doing it with perl instead of vi, and it's working OK.

Thanks for the "%" tip, BTW; didn't know that one.
     
samthecat
Fresh-Faced Recruit
Join Date: Sep 2002
Location: Sydney, Australia
Status: Offline
Reply With Quote
Dec 28, 2002, 06:32 AM
 
i think why may be buried in bill joy's brain...

you could try vim.org if you are really interested (i think they have a mailing list)...

just fyi, i find tr a good command for simple stuff like this...

tr '\n' '\r' < srcfile > outfile

will convert newlines to carriage returns.


(i'll say 'you're welcome' on paul's behalf about the %)

cheers
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:07 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,