When I open the file in vim, I am seeing strange ^M characters.

Unfortunately, the world's favorite search engine does not do well with special characters in queries, so I'm asking here:

  • What is this ^M character?

  • How could it have gotten there?

  • How do I get rid of it?

  • Vlastimil that was a pointless edit. Got is proper past tense of get in British English. – Jesse_b Feb 9 at 3:55

11 Answers 11

up vote 79 down vote accepted

The ^M is a carriage-return character. If you see this, you're probably looking at a file that originated in the DOS/Windows world, where an end-of-line is marked by a carriage return/newline pair, whereas in the Unix world, end-of-line is marked by a single newline.

Read this article for more detail, and also the Wikipedia entry for newline.

This article discusses how to set up vim to transparently edit files with different end-of-line markers.

If you have a file with ^M at the end of some lines and you want to get rid of them, use this in Vim:

:s/^M$//

(Press Ctrl+V Ctrl+M to insert that ^M.)

  • 8
    Try :%s/^M/\r/g instead to remove ^M and replace ^M with newline character \r. Without %, the command applies for current line only. And I came across some examples where ^M is not at end of line, such as The first line.^MThe second line. – George Apr 14 '15 at 4:29
  • Or if you don't want loads of line breaks you could just do :%s/^M/ – carefulnow1 Nov 21 '16 at 8:52
  • 2
    If it's just a carriage return by itself, that might be the classic (pre-Unix) Macintosh line break. Even some newer programs like Excel 2007 for Mac do that for some reason. – sudo Apr 18 '17 at 17:22
  • @larsks But altap.cz/salamander/help/salamand/appendix_txtfiles says that ^M is used in UNIX – Vivek Feb 16 at 7:23
  • The article to which you have linked says that \n is used in Unix, which is correct. That is ASCII code 10, otherwise known as ^J. – larsks Feb 16 at 21:05

Most UNIX operating systems have a utility called dos2unix that will convert the CRLF to LF. The other answers cover the "what are they" question.

A simpler way to do this is to use the following command:

dos2unix filename

This command works with path patterns as well, Eg

dos2unix path/name*

If it doesn't work, try using different mode:

dos2unix -c mac filename
  • -c Set conversion mode. Where CONVMODE is one of: ascii, 7bit, iso, mac with ascii being the default.

This worked for me

:e ++ff=dos 

The :e ++ff=dos command tells Vim to read the file again, forcing dos file format. Vim will remove CRLF and LF-only line endings, leaving only the text of each line in the buffer.

then

:set ff=unix 

and finally

:wq 
  • 1
    thanks! this worked for me but the accepted solution didnt – Mike Palmice May 30 at 18:03
  • Does ff refers to file format? What about ++ff? – KcFnMi Aug 23 at 18:10
  • The :e ++ff=dos command tells Vim to read the file again, forcing dos file format. Vim will remove CRLF and LF-only line endings, leaving only the text of each line in the buffer. – Stryker Aug 29 at 20:05

Another way to get rid of carriage returns is with the tr command.

I have a small script that look like this

#!/bin/sh
tmpfile=$(mktemp)
tr -d '\r' <"$1" >"$tmpfile"
mv "$tmpfile" "$1"

You can clean this up with sed:

sed -e 's/^M$//' < infile > outfile

The trick is how to enter the carriage-return properly. Generally, you need to type C-v C-m to enter a literal carriage return. You can also have sed work in place with

sed -i.bak -e 's/^M$//' infile

In my case,

Nothing above worked, I had a CSV file copied to Linux machine from my mac and I used all the above commands but nothing helped but the below one

tr "\015" "\n" < inputfile > outputfile

I had a file in which ^M characters were sandwitched between lines something like below

Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKT6TG,TRO_WBFB_500,Trico,CARS,Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKTG0A,TRO_WB_T500,Trico,

What is this ^M?
The ^M is a carriage-return character. If you see this, you're probably looking at a file that originated in the DOS/Windows world, where an end-of-line is marked by a carriage return/newline pair, whereas in the Unix world, end-of-line is marked by a single newline.

How could it have got there?
When there is change in file format.

How do I get rid of it?
open your file with

vim -b FILE_PATH

save it with following command

:%s/^M//g
  • You've got a typo in open your filr with. – Mateusz Piotrowski Jan 27 '16 at 20:48
  • 2
    This answer does not add anything to the other answers. The first paragraph is an almost verbatim copy from the accepted answer. The given code will not save anything, but just remove all carriage return characters from all lines. And I am not sure how opening the file in binary mode will help here. – Dubu Jan 28 '16 at 7:58

You can use Vim in Ex mode:

ex -bsc '%s/\r//|x' file
  1. -b binary mode

  2. % select all lines

  3. s substitute

  4. \r carriage return

  5. x save and close

In the past, I have seen even configuration files are not parsed properly and complain about whitespace, but if you vi and do a set list it won't show the whitespace, grep filename [[space]] will show you ^M

that's when dos2unix file helps

None of the options here worked for me.

Finally opened the file in MS Word, and voila the ^M disappeared. Cut and paste to any other editor of your choice.

  • How much time did it take you to do that compared to a command while you're already in the file, imagine you're working with a large group of file. You'd be wasting a lot of time! Also depend on the file size, it is going to be cumbersome,. – amrx Jul 15 at 0:39

protected by Community Aug 1 '17 at 9:46

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.