tags:

views:

274

answers:

5

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  A: 

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

Adam Lassek
+13  A: 

As a command, type

:%s/^M$//

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

Tobias Baaz
also, :%s/\r$// to avoid the ^V^M hassle
glenn jackman
didn't know that glenn, thanks for the tip
Tobias Baaz
+1  A: 

If you're FTPing these files from one box to another make sure to use ASCII transfers.

Spencer Ruport
+1  A: 

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

Ted Percival
+9  A: 

You can do this:

:set fileformats=dos

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

jqno