^M at the end of every line in vim

DHamrick picture DHamrick · Jul 10, 2009 · Viewed 84.7k times · Source

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?

Answer

Tobias Baaz picture Tobias Baaz · Jul 10, 2009

As a command, type

:%s/^M$//

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