Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?
  • How could it have got there?
  • How do I get rid of it?
share|improve this question

5 Answers 5

up vote 21 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:


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

share|improve this answer

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.

share|improve this answer

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
share|improve this answer

Another way to get rid of stupid chars is with the tr command.

I have a small script that look like this

cat $1 | tr -d '\r' > ~/.tmp_file.txt
mv ~/.tmp_file.txt $1
share|improve this answer

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

dos2unix   filename
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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