^M at the end of every line in vim

Viewed 93168

When I am editing source files using vim and other editors sometimes at the end of the line I get these ^M characters at the end of each line. I think that it has something to do with editing a file in windows and then in linux. How can I remove all of these automatically?

9 Answers

As a command, type

:%s/^M$//

(To get ^M, press ^V ^M, where ^ is CTRL on most keyboards)

You can do this:

:set fileformats=dos

It will hide the ^M's, without touching the file.

There's a program called dos2unix that should strip those for you. Windows uses different line-ending characters which is why that happens.

I tend to run afflicted files through fromdos before reopening them. fromdos is part of the tofrodos package.

The origin of the problem may have been through an FTP transfer. When you FTP these files from one box to another, make sure to use ASCII transfers. Use the command "ASC."

Related