emacs 文件中不断出现的这些 ^M 是什么?

发布于 2024-08-13 04:42:27 字数 262 浏览 5 评论 0原文

我认为这可能与 TextMate 有关,但我们在一个小团队中工作,并且在 git 中几乎相同的文件的全文件冲突方面遇到了一些问题 - 在一个分支中,文件的每一行都附加了 ^M。

这个神秘的 ^M 字符应该做什么?它来自哪里?

我们的开发人员在 Windows/Mac 上使用 emacs,在 Mac 上使用 TextMate,在 Mac 上使用 coda,有时还使用 wp-admin 文本编辑器。

有人曾经遇到过由其中之一引起的这个问题吗?

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 – in one branch the files have a ^M appended to each line.

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.

Did anybody ever have this issue stemming from one of those?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

孤云独去闲 2024-08-20 04:42:27

git-config 中,将 core.autocrlf 设置为 true 使 git 自动为您的平台正确转换行结尾,例如,运行以下命令进行全局设置:

git config --global core.autocrlf true

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 --global core.autocrlf true
離殇 2024-08-20 04:42:27

有人没有正确转换行结束符

我认为是 Windows 人员,因为他们喜欢 CRLF。 Unix 喜欢 LF,Mac 喜欢 CR,直到它被展示为 Unix 方式。

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.

孤千羽 2024-08-20 04:42:27

要使 ^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/

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/

鸠书 2024-08-20 04:42:27

^M0x0d,即回车符。如果您的显示看起来像这样

line 1^M
line 2^M

,那么该文件一定来自 Windows,因为 Windows 上的标准换行符序列是 CR LF (0x0d 0x0a),而在 Unices 上,标准换行序列仅由 LF 组成。

如果文件来自 Mac OS 9 或更早版本的系统,您会看到它,

line 1^Mline 2^M

因为回车符后面不会有换行符。

^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.

此生挚爱伱 2024-08-20 04:42:27

我在 Mac OS 上使用 Android Studio (JetBrains IntelliJ IDEA),我的问题是 ^M 开始出现在我在 GitHub 上的拉取请求。
对我有用的是更改文件的行分隔符。

在编辑器中打开所需的文件转到
文件转到
行分隔符然后
选择最适合您的选项
(对我来说是 LF - Unix and OS X(\n)

根据下一篇文章,这个问题是操作系统之间行尾混淆的结果:
http://jonathonstaff.com/blog/issues-with-line-endings/

您可以在这里找到更多信息:
https://www.jetbrains.com/help/idea/configuring -line-separators.html#d84378e48

在此处输入图像描述

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

夕嗳→ 2024-08-20 04:42:27

它们与 DOS 风格的行结尾和 Unix 风格的区别有关。查看维基百科文章。您也许可以找到 dos2unix 工具来提供帮助,或者只需编写一个小脚本来自行修复它们。

编辑:我在此处找到了以下Python示例代码:

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

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', '' )
心作怪 2024-08-20 04:42:27

除了查询替换之外,您还可以使用
Mx 删除尾随空白

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

浅忆 2024-08-20 04:42:27

Emacs 中行尾的 ^M 表示回车符 (\r) 后跟换行符 (\n)。如果有人在 Windows 上编辑文件(其中行尾是回车符和换行符的组合),而您在 Unix 或 Linux 中编辑文件(其中行尾只是换行符),您经常会看到这种情况。

字符的组合通常是无害的。如果您使用源代码管理,则可以配置文本文件签入格式,以便为您神奇地调整行。或者,您可以使用签入和签出触发器来自动为您“修复”文件。或者,您可以使用 dos2unix 之类的工具来手动调整。

^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.

你与昨日 2024-08-20 04:42:27

将以下内容放入您的~/.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 "")))

,然后您就可以简单地使用Mx dos2unix

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.

青瓷清茶倾城歌 2024-08-20 04:42:27

我不久前遇到了这个问题。 ^M 代表回车符,通过 Ctrl-Q Ctrl-M 进行搜索(这将创建一个文字 ^M)将允许您在 Emacs 中获取该字符的句柄。我沿着这些思路做了一些事情:

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

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]
雨轻弹 2024-08-20 04:42:27

如果您的系统上没有安装 dos2unix 实用程序,您可以创建自己的实用程序来删除 Windows 行尾字符:

vi ~/dos2unix.bash:

使用以下内容

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

在 ~/.bashrc 中添加行:

alias 'dos2unix=~/dos2unix.bash'

Applying

dos2unix file_from_PC.txt

will remove ^Mcharacter at lineends在 file_from_PC.txt 中。您可以使用 cat 检查是否有这些:

cat -v file_from_PC.txt

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
飘逸的'云 2024-08-20 04:42:27

正如大家都提到的。这是不同的行结束样式。
MacOSX 使用 Unix 行结尾 - 即 LF(换行)。

Windows 同时使用 CR(回车符)和 CR(回车符)。 LF(换行)作为行结束符。
由于您同时使用 Windows 和 Mac,这就是问题的根源。

如果您在 Windows 中创建一个文件,然后将其带到 Mac 上,您可能会在行尾看到这些 ^M 字符。

如果你想删除它们,你可以在 emacs 中轻松完成。只需突出显示并复制 ^M 字符,然后执行查询-将 ^M 替换为即可。

编辑:
其他一些可能有帮助的链接。
链接

此链接可帮助您配置 emacs 以使用特定类型的行结束样式。
http://www.emacswiki.org/emacs/EndOfLineTips

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.
Link

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

如梦 2024-08-20 04:42:27

另请参阅:

在 emacs 中隐藏 ^M

如果您选择删除 ^M 字符并且重新提交给您的团队。他们可能会看到一个没有回车符的文件。

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.

末が日狂欢 2024-08-20 04:42:27

只需使用 emacs 命令一句即可摆脱 ^Ms 的最直接方法之一:

    C-x h C-u M-| dos2unix    

分析:

    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 平台具有开箱即用的 dos2unix 实用程序,包括 Mac(带有 <代码>酿造)。在 Windows 下,它也被广泛使用(MSYS2、Cygwin、用户贡献等其他的)。

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).

明天过后 2024-08-20 04:42:27

我的解决方案是使用此 Emacs Wiki 文章 中找到的以下 elisp 函数。

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

在缓冲区上执行函数Mx dos2unix并保存文件,所有^M都会消失。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文