[git] What are these ^M's that keep showing up in my files in emacs?

So I think it may have to do with textmate, but we work in a small team and are having some issues with full-file conflicts of nearly identical files in git because each line of one branch has a ^M appended to it.

What is this mysterious ^M character supposed to do, and where could it be coming from?

Our developers use emacs on Windows/Mac, TextMate on Mac, coda on Mac, and occasionally the wp-admin text editor.

Anybody ever have this issue stemming from one of those?

This question is related to git emacs newline

The answer is


One of the most straightforward ways of gettings rid of ^Ms with just an emacs command one-liner:

    C-x h C-u M-| dos2unix    

Analysis:

    C-x h: select current buffer
    C-u: apply following command as a filter, redirecting its output to replace current buffer
    M-| dos2unix: performs `dos2unix` [current buffer]

*nix platforms have the dos2unix utility out-of-the-box, including Mac (with brew). Under Windows, it is widely available too (MSYS2, Cygwin, user-contributed, among others).


They have to do with the difference between DOS style line endings and Unix style. Check out the Wikipedia article. You may be able to find a dos2unix tool to help, or simply write a small script to fix them yourself.

Edit: I found the following Python sample code here:

string.replace( str, '\r', '' )

To make the ^M disappear in git, type:

git config --global core.whitespace cr-at-eol

Credits: https://lostechies.com/keithdahlby/2011/04/06/windows-git-tip-hide-carriage-return-in-diff/


The solution for me was to use the following elisp function found in this Emacs Wiki Article.

 (defun dos2unix ()
      "Not exactly but it's easier to remember"
      (interactive)
      (set-buffer-file-coding-system 'unix 't) )

Execute the function M-x dos2unix on the buffer and save the file, all ^M will disappear.


As everyone has mentioned. It's different line ending style. MacOSX uses Unix line endings - i.e. LF (line feed).

Windows uses both CR (carriage return) & LF (line feed) as a line ending. Since you're using both windows and mac thats where the problem stems from.

If you create a file in windows and then bring it onto the mac you might see these ^M characters at the end of the lines.

If you want to remove them you can do this very easily in emacs. Just highlight and copy the ^M character and do a query-replace ^M with and you'e done.

EDIT: Some other links that may be of help. http://xahlee.org/emacs/emacs_adv_tips.html

This one helps you configure emacs to use a particular type of line-ending style. http://www.emacswiki.org/emacs/EndOfLineTips


Someone is not converting their line-ending characters correctly.

I assume it's the Windows folk as they love their CRLF. Unix loves LF and Mac loved CR until it was shown the Unix way.


instead of query-replace you may also use M-x delete-trailing-whitespace


^M is 0x0d, i.e. the carriage return character. If your display looks like

line 1^M
line 2^M

then the file must have come from Windows because the standard newline sequence on Windows is CR LF (0x0d 0x0a) whereas the standard newline sequence consists solely of LF on Unices.

If the file had come from a Mac OS 9 or earlier system, you would see it as

line 1^Mline 2^M

because there would be no line feeds following the carriage returns.


^M at the end of line in Emacs is indicating a carriage return (\r) followed by a line feed (\n). You'll often see this if one person edits files on Windows (where end of line is the combination of carriage return and newline characters) and you edit in Unix or Linux (where end of line is only a newline character).

The combination of characters is usually not harmful. If you're using source control, you may be able to configure the text file checkin format so that lines are magically adjusted for you. Alternatively, you may be able to use checkin and checkout triggers that will automatically "fix" the files for you. Or, you might just use a tool like dos2unix to manually adjust things.


I ran into this issue a while back. The ^M represents a Carriage Return, and searching on Ctrl-Q Ctrl-M (This creates a literal ^M) will allow you get a handle on this character within Emacs. I did something along these lines:

M-x replace-string [ENTER] C-q C-m [ENTER] \n [ENTER]

If you don't have dos2unix utility installed on your system, you can create your own to get rid of Windows endline characters:

vi ~/dos2unix.bash:

with the following content

#!/bin/bash
tr -d '\r' < $1 > repl.tmp
mv -f repl.tmp $1

In your ~/.bashrc, add the line:

alias 'dos2unix=~/dos2unix.bash'

Applying

dos2unix file_from_PC.txt

will remove ^M characters at lines ends in file_from_PC.txt. You can check if you have those or not by using cat:

cat -v file_from_PC.txt

I'm using Android Studio (JetBrains IntelliJ IDEA) on Mac OS and my problem was that ^M started to show up in some files in my pull request on GitHub. What worked for me was changing line separator for a file.

Open the desired file in the editor go to File go to Line Separators then choose the best option for you (for me it was LF - Unix and OS X(\n) )

According to the next article this problem is a result of confused line endings between operating systems: http://jonathonstaff.com/blog/issues-with-line-endings/

And more information you can find here: https://www.jetbrains.com/help/idea/configuring-line-separators.html#d84378e48

enter image description here


Pot the following in your ~/.emacs (or eqiuvalent)

(defun dos2unix ()
  "Replace DOS eolns CR LF with Unix eolns CR"
  (interactive)
    (goto-char (point-min))
      (while (search-forward "\r" nil t) (replace-match "")))

and then you would be able to simply use M-x dos2unix.


See also:

Hiding ^M in emacs

Be careful if you choose to remove the ^M characters and resubmit to your team. They may see a file without carriage returns afterward.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to emacs

Step-by-step debugging with IPython How to open Emacs inside Bash Where does this come from: -*- coding: utf-8 -*- How do you 'redo' changes after 'undo' with Emacs? How do I make Git use the editor of my choice for commits? How can I reload .emacs after changing it? What are these ^M's that keep showing up in my files in emacs? Differences between Emacs and Vim How to replace a character with a newline in Emacs? How to set the font size in Emacs?

Examples related to newline

How can I insert a line break into a <Text> component in React Native? Print "\n" or newline characters as part of the output on terminal Using tr to replace newline with space How to write one new line in Bitbucket markdown? Line break in SSRS expression How to insert a new line in Linux shell script? Replace CRLF using powershell How to write new line character to a file in Java What is the newline character in the C language: \r or \n? How to print values separated by spaces instead of new lines in Python 2.7