git 在我的 emacs 文件中不断出现的这些 ^M 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1822849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are these ^M's that keep showing up in my files in emacs?
提问by Neil Sarkar
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.
所以我认为这可能与 textmate 有关,但我们在一个小团队中工作,并且在 git 中几乎相同文件的完整文件冲突方面存在一些问题,因为一个分支的每一行都附加了一个 ^M。
What is this mysterious ^M
character supposed to do, and where could it be coming from?
这个神秘^M
人物应该做什么,它可能来自哪里?
Our developers use emacs on Windows/Mac, TextMate on Mac, coda on Mac, and occasionally the wp-admin text editor.
我们的开发人员在 Windows/Mac 上使用 emacs,在 Mac 上使用 TextMate,在 Mac 上使用 coda,偶尔使用 wp-admin 文本编辑器。
Anybody ever have this issue stemming from one of those?
有没有人遇到过这个问题源于其中之一?
采纳答案by Josh Lee
In git-config, set core.autocrlf
to true
to make git automatically convert line endings correctly for your platform, e.g. run this command for a global setting:
在git-config 中,设置core.autocrlf
为true
使 git 自动为您的平台正确转换行尾,例如,为全局设置运行此命令:
git config --global core.autocrlf true
回答by Broam
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.
我认为这是 Windows 用户,因为他们喜欢他们的 CRLF。Unix 喜欢 LF,Mac 喜欢 CR,直到它被展示为 Unix 方式。
回答by Sinan ünür
^M
is 0x0d
, i.e. the carriage return character. If your display looks like
^M
is 0x0d
,即回车符。如果您的显示器看起来像
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.
那么该文件必须来自 Windows,因为 Windows 上的标准换行符序列是CR
LF
( 0x0d 0x0a
) 而标准换行符序列仅由LF
Unices 组成。
If the file had come from a Mac OS 9 or earlier system, you would see it as
如果文件来自 Mac OS 9 或更早的系统,您会看到它
line 1^Mline 2^M
because there would be no line feeds following the carriage returns.
因为回车后不会有换行符。
回答by bonif
To make the ^M disappear in git, type:
要使 ^M 在 git 中消失,请键入:
git config --global core.whitespace cr-at-eol
Credits: https://lostechies.com/keithdahlby/2011/04/06/windows-git-tip-hide-carriage-return-in-diff/
学分:https: //lostechies.com/keithdahlby/2011/04/06/windows-git-tip-hide-carriage-return-in-diff/
回答by Parappa
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.
它们与 DOS 风格的行尾和 Unix 风格之间的区别有关。查看维基百科文章。您也许可以找到一个 dos2unix 工具来提供帮助,或者只是编写一个小脚本来自己修复它们。
Edit: I found the following Python sample code here:
编辑:我在这里找到了以下 Python 示例代码:
string.replace( str, '\r', '' )
回答by CookieMonster
I'm using Android Studio(JetBrains IntelliJ IDEA) on Mac OSand 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.
我在Mac OS上使用Android Studio(JetBrains IntelliJ IDEA) ,我的问题是 ^M 开始出现在我在GitHub 上的拉取请求中的某些文件中。对我有用的是更改文件的行分隔符。
Open the desired file in the editorgo to Filego to Line Separatorsthen choose the best option for you(for me it was LF - Unix and OS X(\n))
在编辑器中打开所需的文件,转到 文件转到 行分隔符,然后 为您选择最佳选项(对我来说是LF - Unix 和 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/
根据下一篇文章,这个问题是操作系统之间行尾混淆的结果: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
您可以在此处找到更多信息:https: //www.jetbrains.com/help/idea/configuring-line-separators.html#d84378e48
回答by kaineer
instead of query-replace you may also use M-x delete-trailing-whitespace
您也可以使用 Mx delete-trailing-whitespace 代替查询替换
回答by Jakub Nar?bski
Pot the following in your ~/.emacs
(or eqiuvalent)
在您的~/.emacs
(或等效的)中放入以下内容
(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
.
然后你就可以简单地使用M-x dos2unix
.
回答by atk
^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).
^M
Emacs 中的行尾表示回车 (\r) 后跟换行符 (\n)。如果一个人在 Windows 上编辑文件(行尾是回车符和换行符的组合),而您在 Unix 或 Linux 上编辑(行尾只是换行符),您会经常看到这一点。
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 dos2unixto manually adjust things.
字符的组合通常是无害的。如果您使用源代码管理,您可以配置文本文件签入格式,以便为您神奇地调整行。或者,您可以使用签入和签出触发器自动为您“修复”文件。或者,您可能只是使用dos2unix 之类的工具来手动调整内容。
回答by Matt
As everyone has mentioned. It's different line ending style. MacOSX uses Unix line endings - i.e. LF (line feed).
正如大家所提到的。这是不同的行尾样式。MacOSX 使用 Unix 行结束符 - 即 LF(换行符)。
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.
Windows 使用 CR(回车)和 LF(换行)作为行尾。由于您同时使用 windows 和 mac,这就是问题的根源。
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.
如果您在 Windows 中创建一个文件,然后将其带到 mac 上,您可能会在行尾看到这些 ^M 字符。
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.
如果你想删除它们,你可以在 emacs 中很容易地做到这一点。只需突出显示并复制 ^M 字符,然后执行查询替换 ^M 即可。
EDIT: Some other links that may be of help. http://xahlee.org/emacs/emacs_adv_tips.html
编辑:其他一些可能有帮助的链接。 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
这可以帮助您配置 emacs 以使用特定类型的行尾样式。 http://www.emacswiki.org/emacs/EndOfLineTips