Vim 中每行末尾的 ^M

发布于 2024-07-26 10:52:54 字数 118 浏览 3 评论 0原文

当我使用 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 技术交流群。

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

发布评论

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

评论(11

望她远 2024-08-02 10:52:54

作为命令,键入

:%s/^M$//

(要获取 ^M,请按 ^V ^M,其中 ^ 在大多数键盘上是 CTRL)

As a command, type

:%s/^M$//

(To get ^M, press ^V ^M, where ^ is CTRL on most keyboards)

不疑不惑不回忆 2024-08-02 10:52:54

去除 DOS 行结尾的一种简单方法是使用 ff 选项:

:set ff=unix
:wq

现在您的文件又回到了老式的 Unix 方式。

如果您想添加 DOS 行结尾(为了让打印机满意,或者与没有好工具的 Windows 朋友传输文件),您可以轻松地采取相反的方向:

:set ff=dos
:wq

One easy way to strip out the DOS line endings is to use the ff option:

:set ff=unix
:wq

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:

:set ff=dos
:wq
帅哥哥的热头脑 2024-08-02 10:52:54

您可以这样做:

:set fileformats=dos

它将隐藏 ^M,而不触及文件。

You can do this:

:set fileformats=dos

It will hide the ^M's, without touching the file.

献世佛 2024-08-02 10:52:54

有一个名为 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.

︶葆Ⅱㄣ 2024-08-02 10:52:54

这对我来说在一个所有内容都在一行的文件中有效:

首先找到所有匹配项

:%s/^M//

(要获得 ^M,请按 ^V ^M,其中 ^ 在大多数情况下是 Ctrl键盘)

然后用换行符替换

:%s//\r/g

组合命令将是:

:%s/^M/\r/g

This worked for me in a file that had everything on one line:

First find all matches

:%s/^M//

(To get ^M, press ^V ^M, where ^ is Ctrl on most keyboards)

Then replace with newlines

:%s//\r/g

Combined command would be:

:%s/^M/\r/g
双手揣兜 2024-08-02 10:52:54

我倾向于在重新打开受影响的文件之前通过 fromdos 运行它们。 fromdostfrodos 包。

I tend to run afflicted files through fromdos before reopening them. fromdos is part of the tofrodos package.

陪我终i 2024-08-02 10:52:54

问题的根源可能是通过 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."

未央 2024-08-02 10:52:54
" put this in your ~/.vimrc file and :source ~/.vimrc
" then you can do: Dos2Unix
" dos2unix ^M
fun! Dos2unixFunction()
    let _s=@/
    let l = line(".")
    let c = col(".")
    try
        set ff=unix
        w!
        "%s/\%x0d$//e
    catch /E32:/
        echo "Sorry, first save the file."
    endtry
    let @/=_s
    call cursor(l, c)
endfun
com! Dos2Unix keepjumps call Dos2unixFunction()
" put this in your ~/.vimrc file and :source ~/.vimrc
" then you can do: Dos2Unix
" dos2unix ^M
fun! Dos2unixFunction()
    let _s=@/
    let l = line(".")
    let c = col(".")
    try
        set ff=unix
        w!
        "%s/\%x0d$//e
    catch /E32:/
        echo "Sorry, first save the file."
    endtry
    let @/=_s
    call cursor(l, c)
endfun
com! Dos2Unix keepjumps call Dos2unixFunction()
末が日狂欢 2024-08-02 10:52:54

mcedit:shift+f2,设置unix格式(LF),ok

mcedit: shift+f2, set unix format (LF), ok

乖乖 2024-08-02 10:52:54

我的情况是,我是在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 add

My 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

尽揽少女心 2024-08-02 10:52:54
:%s/\r$//

应删除每行末尾的回车符 \r(显示为 ^M)。

:%s/\r$//

Should remove the carriage return \r (displayed as ^M) at the end of every line.

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