gVim showing carriage return (^M) even when file mode is explicitly DOS

VimCarriage ReturnLine Endings

Vim Problem Overview


I'm using gVim on Windows. My code shows ^M characters at the end of lines. I used :set ff=dos to no avail. The ^M characters remain for existing lines, but don't show up for newlines I enter. I've switched modes to mac (shows ^J characters) and unix (also shows ^M characters) and back to dos. Has anyone else seen this?

Vim Solutions


Solution 1 - Vim

This happens when you have a mixture of Windows line endings and Unix ones. If you have 100 lines, 99 are \r\n and one is \n, you'll see 99 ^M characters. The fix is to find that one line and replace it. Or run dos2unix on the file. You can replace the Windows line endings with:

:%s/\r\(\n\)/\1/g

Solution 2 - Vim

You can also run:

:e ++ff=dos

To remove the ^M: See File format – Vim Tips Wiki.

Solution 3 - Vim

I usually use the following to cleanup my line endings:

:g/^M$/s///

To get the ctrl-M I usually type ctrl-Q, then ctrl-M and it puts it in. (In some environments it may be ctrl-V then ctrl-M.) I don't know why, but I find that one easier to remember than rq's.

Don't forget to do :set ff=dos as well, or you'll end up saving with UNIX line endings still.

Solution 4 - Vim

I know this has already been answered, but a trick I use is

:%s/\r/\r/g

This replaces the unix carriage returns with the windows CRLF. Just added in case anyone else had issues.

Solution 5 - Vim

You can ignore these chars!

put this into your vimrc

> match Ignore /\r$/

Solution 6 - Vim

Actually what worked for me (on 64-bit windows, gVIM: 7.2 ) was:

:set ffs=dos

not just: ff

Solution 7 - Vim

Running Vim 7.3 on Windows 7. I used the following command:

:%s/^M/\r/g

To create the ^M I typed in CTRL+Q then CTRL+M.

Solution 8 - Vim

This is probably a bit simple for many of you but on the off chance it's useful.

Based on richq's answer, I found these to be useful in my vimrc. Note, the second one is commented out normally because it makes dd a bit confusing since Vim will wait for another key stroke to work out if it's the mapped ex command.

function! D2u()
execute '%s/\r(\n)/\1/g'
endfunction
"map d2u :%s/\r(\n)/\1/g

The first is run by typing call D2u() into ex and the second by pressing D2u in edit mode.

Solution 9 - Vim

These are extra CR line endings usually because of a using a file on mixed UNIX/DOS systems.

Possible the shortest answer to remove a single ^M from the end of each line, and what I use, is:

:%s/\r

which is equivalent to:

:%s/\r//

but the end slashes aren't required (they're assumed).

Solution 10 - Vim

tried a lot of things but the following worked

:%s/\r/\r/g

note: use g if you want the effect on the whole file

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
QuestionJerphView Question on Stackoverflow
Solution 1 - VimrichqView Answer on Stackoverflow
Solution 2 - VimbeckelmwView Answer on Stackoverflow
Solution 3 - VimEvanView Answer on Stackoverflow
Solution 4 - VimdsrekabView Answer on Stackoverflow
Solution 5 - VimIvanMView Answer on Stackoverflow
Solution 6 - Vimra170View Answer on Stackoverflow
Solution 7 - Vimddelrio1986View Answer on Stackoverflow
Solution 8 - VimPeter ShannonView Answer on Stackoverflow
Solution 9 - VimthebriguyView Answer on Stackoverflow
Solution 10 - VimAtul SubhashView Answer on Stackoverflow