^M at the end of every line in vim

WindowsLinuxVim

Windows Problem Overview


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?

Windows Solutions


Solution 1 - Windows

As a command, type

:%s/^M$//

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

Solution 2 - Windows

One easy way to strip out the DOS line endings is to use the ff option:

:set ff=unix
:wq

Now your file is back to the good-old-Unix-way.

If you want to add the DOS line-endings (to keep a printer happy, or transfer files with Windows friends who don't have nice tools) you can go the opposite direction easily:

:set ff=dos
:wq

Solution 3 - Windows

You can do this:

:set fileformats=dos

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

Solution 4 - Windows

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

Solution 5 - Windows

This worked for me in a file that had everything on one line:

First find all matches

:%s/^M//

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

Then replace with newlines

:%s//\r/g

Combined command would be:

:%s/^M/\r/g

Solution 6 - Windows

I tend to run afflicted files through fromdos before reopening them. fromdos is part of the http://www.thefreecountry.com/tofrodos/index.shtml"><strong>tofrodos</strong></a> package.

Solution 7 - Windows

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."

Solution 8 - Windows

" put this in your ~/.vimrc file and :source ~/.vimrc
" then you can do: Dos2Unix
" dos2unix ^M
fun! Dos2unixFunction()
	let _s=@/
	let l = line(".")
	let c = col(".")
	try
		set ff=unix
		w!
		"%s/\%x0d$//e
	catch /E32:/
		echo "Sorry, first save the file."
	endtry
	let @/=_s
	call cursor(l, c)
endfun
com! Dos2Unix keepjumps call Dos2unixFunction()

Solution 9 - Windows

mcedit: shift+f2, set unix format (LF), ok

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDHamrickView Question on Stackoverflow
Solution 1 - WindowsTobias BaazView Answer on Stackoverflow
Solution 2 - WindowssarnoldView Answer on Stackoverflow
Solution 3 - WindowsjqnoView Answer on Stackoverflow
Solution 4 - WindowsAdam LassekView Answer on Stackoverflow
Solution 5 - WindowsTheRealKingKView Answer on Stackoverflow
Solution 6 - WindowsT PercivalView Answer on Stackoverflow
Solution 7 - WindowsSpencer RuportView Answer on Stackoverflow
Solution 8 - WindowsSergioAraujoView Answer on Stackoverflow
Solution 9 - WindowsVasily ChapaevView Answer on Stackoverflow