Saturday 31 August 2019

text editors - `^M` in many lines in source code files. How can I remove them?


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?



Answer



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.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...