Vim 中每行末尾的 ^M
当我使用 Vim 和其他编辑器编辑源文件时,有时我会在每行末尾看到这些 ^M
字符。 我认为这与在 Windows 上编辑文件然后在 Linux 上编辑文件有关。 我怎样才能自动删除所有这些?
When I am editing source files using Vim and other editors, sometimes I get these ^M
characters at the end of each line.
I think that it has something to do with editing a file on Windows and then on Linux.
How can I remove all of these automatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
作为命令,键入
(要获取 ^M,请按 ^V ^M,其中 ^ 在大多数键盘上是 CTRL)
As a command, type
(To get ^M, press ^V ^M, where ^ is CTRL on most keyboards)
去除 DOS 行结尾的一种简单方法是使用 ff 选项:
现在您的文件又回到了老式的 Unix 方式。
如果您想添加 DOS 行结尾(为了让打印机满意,或者与没有好工具的 Windows 朋友传输文件),您可以轻松地采取相反的方向:
One easy way to strip out the DOS line endings is to use the
ff
option:Now your file is back to the good-old-Unix-way.
If you want to add the DOS line-endings (to keep a printer happy, or transfer files with Windows friends who don't have nice tools) you can go the opposite direction easily:
您可以这样做:
它将隐藏
^M
,而不触及文件。You can do this:
It will hide the
^M
's, without touching the file.有一个名为 dos2unix 的程序应该为您删除这些内容。 Windows 使用不同的行结束字符,这就是发生这种情况的原因。
There's a program called dos2unix that should strip those for you. Windows uses different line-ending characters which is why that happens.
这对我来说在一个所有内容都在一行的文件中有效:
首先找到所有匹配项
(要获得
^M
,请按 ^V ^M,其中 ^ 在大多数情况下是 Ctrl键盘)然后用换行符替换
组合命令将是:
This worked for me in a file that had everything on one line:
First find all matches
(To get
^M
, press ^V ^M, where ^ is Ctrl on most keyboards)Then replace with newlines
Combined command would be:
我倾向于在重新打开受影响的文件之前通过
fromdos
运行它们。fromdos
是 tfrodos 包。I tend to run afflicted files through
fromdos
before reopening them.fromdos
is part of the tofrodos package.问题的根源可能是通过 FTP 传输。 当您通过 FTP 将这些文件从一个盒子传输到另一个盒子时,请确保使用 ASCII 传输。 使用命令“ASC”。
The origin of the problem may have been through an FTP transfer. When you FTP these files from one box to another, make sure to use ASCII transfers. Use the command "ASC."
mcedit:shift+f2,设置unix格式(LF),ok
mcedit: shift+f2, set unix format (LF), ok
我的情况是,我是在Windows上开发项目,后来跑了一个Linux虚拟机,通过共享文件夹的方式,直接在Linux虚拟机中打开了原来在Windows上的项目。 当我运行 git status 时,会列出大量文件,并且这些文件的每一行都以 ^M 结尾。 我用以下两个命令解决了这个问题:
git status -s | grep "^ M" | grep "^ M" | grep "^ M" | awk '{print $2}' | xargs dos2unix
git status -s | xargs dos2unix
git status -s | xargs dos2unix
grep“^ M”| awk '{print $2}' | xargs git addMy situation is, I was developing my project on Windows, later I ran a Linux virtual machine, and by sharing folders, I directly opened the project that was originally on Windows in the Linux virtual machine. When I ran
git status
, a large number of files were listed, and each line of these files ended with^M
. I solved this issue with the following two commands:git status -s | grep "^ M" | awk '{print $2}' | xargs dos2unix
git status -s | grep "^ M" | awk '{print $2}' | xargs git add
应删除每行末尾的回车符
\r
(显示为^M
)。Should remove the carriage return
\r
(displayed as^M
) at the end of every line.