Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 250
Jerph Avatar asked Apr 28 '09 19:04

Jerph


People also ask

How do I show carriage returns in vim?

For non-Unix style line-endings, the carriage return character \r will be displayed explicitly in vim as ^M .

How do I get rid of M in vim?

How can I remove ^M characters from text files? A. ^M are nothing more than carriage return, so it is easy to use the search and replace function of vim to remove them. That will do the job.

How do I view Crlf in vi?

vi shows newlines (LF character, code x0A ) by showing the subsequent text on the next line. Use the -b switch for binary mode. For example , vi -b filename or vim -b filename -- . It will then show CR characters ( x0D ), which are not normally used in Unix style files, as the characters ^M .

What does M mean in vim?

0xD is the carriage return character. ^M happens to be the way vim displays 0xD (0x0D = 13, M is the 13th letter in the English alphabet). You can remove all the ^M characters by running the following: :%s/^M//g. Where ^M is entered by holding down Ctrl and typing v followed by m , and then releasing Ctrl .


2 Answers

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

like image 100
richq Avatar answered Sep 28 '22 03:09

richq


You can also run:

:e ++ff=dos 

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

like image 23
beckelmw Avatar answered Sep 28 '22 02:09

beckelmw