What is `^M` and how do I get rid of it?

When I open the documents in Vim, I see weird ^M personalities.

However, the globe rates internet search engine does refrain well with special characters in questions, so I'm asking below:

  • What is this ^M personality?

  • Just how could it have arrived?

  • Just how do I remove it?

274
2022-06-06 19:07:46
Source Share
Answers: 5

A less complex means to do this is to make use of the adhering to command:

dos2unix filename

This command collaborates with course patterns too, Eg

dos2unix path/name*

If it does not function, attempt making use of various setting:

dos2unix -c mac filename
  • -c Set conversion setting. Where CONVMODE is just one of: ascii, 7bit, iso, mac with ascii being the default
87
2022-06-06 19:45:42
Source

Most UNIX os have actually an energy called dos2unix that will certainly transform the CRLF to LF. The various other solutions cover the "what are they" inquiry.

48
2022-06-06 19:23:09
Source

You can cleanse this up with sed:

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

The method is just how to enter the carriage - return effectively. Usually, you require to type C-v C-m to enter an actual carriage return. You can additionally have actually sed operate in area with

sed -i.bak -e 's/^M$//' infile
9
2022-06-06 19:22:49
Source

The ^M is a carriage - return personality. If you see this, you are possibly considering a documents that came from the DOS/Windows globe, where an end - of - line is noted by a carriage return/newline set, whereas in the Unix globe, end - of - line is noted by a solitary newline.

Read this article for even more information, as well as additionally the Wikipedia access for newline.

This article reviews just how to set up vim to transparently modify documents with various end - of - line pens.

If you have a documents with ^M at the end of some lines and also you intend to remove them, utilize this in Vim:

:s/^M$//

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

190
2022-06-06 19:20:42
Source

Another means to remove carriage returns is with the tr command.

I have a tiny manuscript that resemble this

#!/bin/sh
tmpfile=$(mktemp)
tr -d '\r' <"$1" >"$tmpfile"
mv "$tmpfile" "$1"
11
2022-06-06 19:19:23
Source