在vim中将DOS行尾转换为Linux行尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/82726/
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
Convert DOS line endings to Linux line endings in vim
提问by Bert Hekman
If I open files I created in Windows, the lines all end with ^M
.
How do I delete these characters all at once?
如果我打开在 Windows 中创建的文件,所有行都以^M
.
如何一次删除这些字符?
采纳答案by pjz
dos2unixis a commandline utility that will do this, or :%s/^M//g
will if you use Ctrl-vCtrl-mto input the ^M, or you can :set ff=unix
and vim will do it for you.
DOS2UNIX的是一个命令行实用工具,将做到这一点,还是:%s/^M//g
会如果你使用Ctrl- vCtrl-m输入^ M,或者你可以:set ff=unix
和vim会为你做它。
Docs on the 'fileformat' setting are here, and the vim wiki has a comprehensive page on line ending conversions.
关于“文件格式”设置的文档在这里,vim wiki 有一个关于行尾转换的综合页面。
Alternately, if you move files back and forth a lot, you might not want to convert them, but rather to do :set ff=dos
, so vim will know it's a DOS file and use DOS conventions for line endings.
或者,如果您来回移动文件很多,您可能不想转换它们,而是要执行:set ff=dos
,因此 vim 将知道它是 DOS 文件并使用 DOS 约定作为行尾。
回答by mercutio
:%s/\r+//g
In Vim, that strips all carriage returns, and leaves only newlines.
在 Vim 中,这会去除所有回车符,只留下换行符。
回答by Rob Wells
:g/Ctrl-v Ctrl-m/s///
CtrlMis the character \r
, or carriage return, which DOS line endings add. CtrlVtells vim to insert a literal CtrlMcharacter at the command line.
CtrlM是\r
DOS 行结尾添加的字符或回车符。CtrlV告诉 vimCtrlM在命令行插入一个文字字符。
Taken as a whole, this command replaces all \r
with nothing, removing them from the ends of lines.
总的来说,这个命令用空替换所有\r
,从行尾删除它们。
回答by JayG
You can use the following command::%s/^V^M//g
where the '^' means use CTRLkey.
您可以使用以下命令::%s/^V^M//g
其中“^”表示使用CTRL密钥。
回答by Dave Webb
With the following command:
使用以下命令:
:%s/^M$//g
Get the ^M
to appear type CtrlVthen CtrlM. CtrlVtells Vim to take the next character entered literally.
然后获取^M
显示类型。 告诉 Vim 按字面意思输入下一个字符。CtrlVCtrlMCtrlV
回答by Alex Gartrell
from: http://vim.wikia.com/wiki/Change_end-of-line_format_for_dos-mac-unix
来自:http: //vim.wikia.com/wiki/Change_end-of-line_format_for_dos-mac-unix
[Esc] :%s/\r$//
[Esc] :%s/\r$//
回答by Sylvain Defresne
I prefer to use the following command :
我更喜欢使用以下命令:
:set fileformat=unix
You can also use mac
or dos
to respectively convert your file to macintosh or MS-DOS/MS-Windows file convention. And it does nothing if the file is already in the correct format.
您还可以使用mac
或dos
分别将您的文件转换为 macintosh 或 MS-DOS/MS-Windows 文件约定。如果文件已经是正确的格式,它什么也不做。
For more information, see the vim help :
有关更多信息,请参阅 vim 帮助:
:help fileformat
回答by Sylvain Defresne
:set fileformat=unix
to convert from dos to unix.
:set fileformat=unix
从 dos 转换到 unix。
回答by Ludvig A. Norin
Change the line endings in the view:
更改视图中的行尾:
:e ++ff=dos
:e ++ff=mac
:e ++ff=unix
This can also be used as saving operation (:w alone will not save using the line endings you see on screen):
这也可以用作保存操作(单独使用 :w 不会使用您在屏幕上看到的行结尾进行保存):
:w ++ff=dos
:w ++ff=mac
:w ++ff=unix
And you can use it from the command-line:
您可以从命令行使用它:
for file in *.cpp
do
vi +':w ++ff=unix' +':q' "$file"
done
回答by Ludvig A. Norin
I typically use
我通常使用
:%s/\r/\r/g
which seems a little odd, but works because of the way that vim matches linefeeds. I also find it easier to remember :)
这看起来有点奇怪,但由于 vim 匹配换行符的方式而起作用。我也发现它更容易记住:)