每行带有 ^M 的文本文件

发布于 2024-09-09 07:52:38 字数 161 浏览 0 评论 0原文

我刚刚从朋友那里得到了一个源代码文件。该文件是在 UNIX 中创建的。当我使用 NotePad++ 在 Windows 中打开它时,每一行都有一个额外的空行。

带着疑惑,我下载了 Vim 并用它打开了该文件。然后我在每行末尾看到一堆 ^M 。

这是什么^M?如何防止它被插入?

I just got a source code file from a friend. The file was created in UNIX. When I opened it in Windows using NotePad++, each line had one extra blank line.

Puzzled, I downloaded Vim and used it to open the file. I then saw a bunch of ^M at the end of each line.

What is this ^M? How do you prevent it from being inserted?

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

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

发布评论

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

评论(3

把回忆走一遍 2024-09-16 07:52:38

这些是 DOS/Windows 风格的行结束符(迂腐地说,这就是它们现在的俗称,但大多数早期的非 UNIX 操作系统,如 CP/M 和 OS/2 也有它们)。在各种 Unices 上,行结束符为 \n。在 DOS/Windows 中,行结束符为 \r\n(CR+LF 或回车换行)。 \r 显示为 ^M。要在 vim 中删除它们,我这样做:

:%s/^M//

您可以通过执行 CTRL+V 然后 CTRL+M< 来获取 ^M /kbd>

如果您使用的是 UNIX 系统,则可以使用 dos2unix

Those are DOS/Windows-style line-endings (to be pedantic that's what they're commonly known as now but most early non-UNIX OSes like CP/M and OS/2 had them as well). On the various Unices, line-endings are \n. In DOS/Windows the line-endings are \r\n (CR+LF or Carriage-Return and Line-Feed). The \r is what shows up as ^M. To remove them in vim, I do:

:%s/^M//

You can get the ^M by doing CTRL+V and then CTRL+M

If you are on a UNIX system, you can use dos2unix.

静若繁花 2024-09-16 07:52:38

它是一个 Windows 制品。您有 2 个选项

  1. 使用可以指定保存格式的编辑器
  2. 在类 Unix 系统上使用 dos2unix 进行转换

its a windows artefact. You have 2 options

  1. Use an editor where you can specify the save format
  2. Use dos2unix on a unix like system to convert
依 靠 2024-09-16 07:52:38

这是一个回车符。 DOS/Windows 编辑器倾向于使用回车符和换行符,Unix 编辑器喜欢仅使用换行符。有些编辑器(例如 geany 和 textpad)可以检测到它并且不会向您显示 ^M,有些编辑器可以让您保存为包含 unix 样式或 crlf 样式的选项。还有一些编译器过去要求文件末尾有 EOF 的问题。

It is a carriage return. DOS/Windows editors tend to use a carriage return and a line feed, Unix editors like to use just the line feed. Some editors like geany and textpad can detect it and dont show you the ^M, and some will let you do a save as where the choices include unix style or crlf style. There is also the issue of having an EOF at the end of the file that some compilers used to require.

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