[英]Why does Vim treat \n as a valid EOL character for files in DOS format?
I was testing how Vim works with EOL characters. 我正在测试Vim如何处理EOL字符。
If I create a file that looks like this: 如果我创建一个看起来像这样的文件:
and then load the file into Vim, explicitly stating that I want the file to be loaded in the dos fileformat (and not in the unix fileformat), the file looks like it has no errors: 然后将文件加载到Vim中,明确表明我希望文件以dos文件格式(而不是unix文件格式)加载,该文件看起来没有错误:
How I would expect the file to be displayed: 我希望该文件如何显示:
Is there a deeper reason for this (ie in how Vim stores newline characters internally) or is this just a convenience mechanism in Vim, in case the person that loads the file, specifies the wrong file format? 是否有更深层的原因(例如,Vim如何在内部存储换行符),或者这仅仅是Vim中的一种便捷机制,以防加载文件的人指定了错误的文件格式? If the former, please say about the newline mechanism a little more in the context of my example.
如果是前者,请在我的示例中再谈一些换行机制。
I think this is what happens. 我认为这是会发生的。 The file format (can be
dos
or unix
) can be detected or manually set when opening a file. 打开文件时,可以检测或手动设置文件格式(可以是
dos
或unix
)。 If it is set to dos
, Vim removes both \\r\\n\\
or \\n
from the file and replace them with its internal form of line ending (If it's unix
, it replaces \\n
only, so \\r\\n
will be left as \\r
which is displayed as ^J
). 如果将其设置为
dos
,Vim将从文件中删除\\r\\n\\
或\\n
并将其替换为其内部行尾形式(如果为unix
,则仅替换\\n
,因此将保留\\r\\n
为\\r
,显示为^J
)。
When you write to file, Vim will replace the internal EOL with the EOL corresponding with your fileformat – CRLF for dos
, LF for unix
. 当您写入文件时,Vim会将内部EOL替换为与文件格式相对应的EOL-CRLF表示
dos
,LF表示unix
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.