Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Someone recently sent me a couple of .cpp source code files. On opening the files with GNU Emacs or GVIM, I found them to have a weird character ^M near the ending of every line.

Here is a screenshot showing what I meanEditor Screenshot

When I open these same source files with Gedit, I do not see these ^M characters. What should I do? There are many files in question here, so opening and doing a search-replace operation by replaceing with an empty string looks like a back-breaking operation.

Strangely enough these files compile cleanly when I compile with GCC. What is going on here. What should I do?

share|improve this question
    
If you will be continually collaborating with this person you should almost certainly do nothing. They do not cause any harm, and if you send any modified files back, then you might cause problems for him, when he tries to use the file. –  Zoredache Feb 29 '12 at 19:38

4 Answers 4

up vote 6 down vote accepted

In Unix, the new line character is \n and whereas in Windows it is \r\n. \r or ^M is the carriage-return character.

If the file is written in an editor in windows mode, each new line character will have a carriage return character along. If you tell your editor (and if it understands) to treat the new line characters as in windows, you would not see the ^M characters any more.

Alternately, you can run dos2unix on your codebase to fix this problem.

And as you have rightly observed, you would not see any problems in compiling because that is not any extra special character in the code but just a mismatch between newline character interpretation in windows and linux.

share|improve this answer
    
More ways to do this: sed.sourceforge.net/sed1line.txt under Convert a file from DOS to UNIX and vice versa –  lupincho Feb 29 '12 at 20:51

You have a source file with mixed Windows and UNIX line endings. While UNIX just used linefeed (sometimes shown as ^J) Windows uses carriage-return + linefeed (sometimes shown as ^M^J). Your editor sees the UNIX lines and says "this must be a UNIX file" but then sees the scattered Windows line endings and says "this is a special character, I must show it".

You can try to convert the file all one way or the other. You can try to use the command dos2unix to convert to UNIX line endings. That will work on the UNIX side, but your Windows side will be confused unless you get a Windows editor that know s UNIX endings.

Or you can use unix2dos and make it all Windows. If you're consistent with the entire file, your UNIX editors (which tend to know Windows endings better than Windows editors know UNIX endings) should be fine. This particular time emacs/gvim was just confused because the file was inconsistent.

As you have seen, the compiler doesn't care what the line ending is.

share|improve this answer

According to this resource, ^M is a carriage return - that's why you can compile cleanly with GCC.

share|improve this answer

The reason you see these in emacs/vim is due to how they read things/how things are encoded. The ^M is actually just a Carriage Return (hitting the enter button).

Anyway, you can try doing (in vim, since I don't know the appropriate command in Emacs) :set ff=unix, and then :w, which should turn it into a Unix format and get rid of it the next time you open it (iirc).

Or, if you want explicitly remove ^M from the document, do a search/replace (in Vim, since I only know that) with :%s/<ctrl + v><ctrl + m>//gc.

( This link shows you the commands on how to do a S/R for returns in Emacs. )

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.