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?
For non-Unix style line-endings, the carriage return character \r will be displayed explicitly in vim as ^M .
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.
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 .
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 .
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
You can also run:
:e ++ff=dos
To remove the ^M
: See File format – Vim Tips Wiki.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With