Linux ^M 在 vi​​m 中每一行的末尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1110678/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:30:35  来源:igfitidea点击:

^M at the end of every line in vim

windowslinuxvim

提问by DHamrick

When I am editing source files using vim and other editors sometimes at the end of the line I get these ^M characters at the end of each line. I think that it has something to do with editing a file in windows and then in linux. How can I remove all of these automatically?

当我使用 vim 和其他编辑器编辑源文件时,有时会在行尾得到这些 ^M 字符。我认为这与在 Windows 和 Linux 中编辑文件有关。如何自动删除所有这些?

采纳答案by Tobias Baaz

As a command, type

作为命令,键入

:%s/^M$//

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

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

回答by Adam Lassek

There's a program called dos2unixthat should strip those for you. Windows uses different line-ending characters which is why that happens.

有一个名为dos2unix的程序可以为您删除这些程序。Windows 使用不同的行结束字符,这就是发生这种情况的原因。

回答by Spencer Ruport

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

问题的根源可能是通过 FTP 传输。当您通过 FTP 将这些文件从一个盒子传输到另一个盒子时,请确保使用 ASCII 传输。使用命令“ASC”。

回答by Ted Percival

I tend to run afflicted files through fromdosbefore reopening them. fromdosis part of the tofrodospackage.

我倾向于fromdos在重新打开它们之前运行受影响的文件。fromdostofrodos软件包的一部分。

回答by jqno

You can do this:

你可以这样做:

:set fileformats=dos

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

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

回答by TheRealKingK

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 Ctrlon most keyboards)

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

Then replace with newlines

然后用换行符替换

:%s//\r/g

Combined command would be:

组合命令将是:

:%s/^M/\r/g

回答by SergioAraujo

" 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()

回答by sarnold

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

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

:set ff=unix
:wq

Now your file is back to the good-old-Unix-way.

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

If you want to addthe 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:

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

:set ff=dos
:wq

回答by Vasily Chapaev

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

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