48

这个问题是我用于“在 vim 中保存文件而不强制在文件末尾添加换行符”烦恼的 Workaround后续问题。

基本上我不能用set noeol在我的.vimrc,因为它什么都不做!

如果我以二进制模式编辑文件,它会做它应该做的事情。(vim -b file而不是vim file

这是为什么?

无论如何,有一个简单的偏好,.vimrc即不在我编辑的每个文件中添加换行符?

另外,如果我开始以二进制模式编辑每个文件,我会遇到什么样的问题?到目前为止,我还没有看到任何区别。

4

4 回答 4

111

Vim 在文件最后一行末尾“添加”的是“换行”字符,不应与“换行”混淆。

换行符”字符或更准确地说“行尾”字符 ( <EOL>) 表示“在此点之后的任何内容都必须被视为在另一行上”。使用这种解释 -<EOL>是行终止符- 文件的最后一行实际上是最后一行带有<EOL>.

问题是大多数编辑器和 IDE 有不同的解释——<EOL>是一个行分隔符——并且,从逻辑上讲,默认不会在新<EOL>文件的最后一行末尾添加<EOL>行”在真正的最后一行之后。

简而言之,Vim 没有添加“新行”:其他编辑器(错误地)将其“新行”解释为“新行”。

但是您可以通过执行以下操作来解决该问题:在编写文件之前,:set binary noeol如果您希望它保持“ <EOL>-free”,请执行此操作。

然而,:h 'binary'关于它的危险有很多话要说,:set binary所以我想说一直“打开”它听起来是个坏主意。

为了说明不同的行为,当您尝试用 连接两个文件时会发生这种情况<EOL>

$ cat file1    $ cat file2         $ cat file1 file2

lorem ipsum    Le tramway jaune    lorem ipsum
dolor sit      avance lentement    dolor sit
amet           dans le             amet
                                   Le tramway jaune
                                   avance lentement 
                                   dans le

这就是当您尝试在没有以下情况下连接两个文件时发生的情况<EOL>

$ cat file1    $ cat file2         $ cat file1 file2

lorem ipsum    Le tramway jaune    lorem ipsum
dolor sit      avance lentement    dolor sit
amet           dans le             ametLe tramway jaune
                                   avance lentement 
                                   dans le

第一个行为在某种程度上是预期的行为,也是 Vim 和许多(如果不是大多数)UNIX-y 程序默认使用终止符<EOL>解释并在最后一行末尾添加一个字符的原因。

下图显示了一个简单的文件,使用<EOL>创建nano(与 Vim 相同)并在 Eclipse、TextMate、Sublime Text、Vim、Xcode 和 TextEdit 中打开。

<停产>

(编辑)此文件中没有第 4 行,并且正确显示该文件的唯一编辑器是 Vim。行号列的唯一目的是提供有关缓冲区的信息。在只有 3 行的情况下显示 4 行是一个严重错误。(完结)

这张图片显示了另一个没有<EOL>使用 Sublime Text 创建并在相同的编辑器/IDE 中打开的简单文件。

否 <EOL>

于 2013-04-25T20:39:29.340 回答
21

从版本 7.4.785 vim 有fixendofline设置。您可以避免binary(有一些副作用)并简单地设置

set noendofline
set nofixendofline
于 2016-09-21T22:16:55.963 回答
4

根据 vimdoc noeol,除非打开模式,否则什么都不做binary

            *'endofline'* *'eol'* *'noendofline'* *'noeol'*
'endofline' 'eol'   boolean (default on)
            local to buffer
            {not in Vi}
    When writing a file and this option is off and the 'binary' option
    is on, no <EOL> will be written for the last line in the file.  This
    option is automatically set when starting to edit a new file, unless
    the file does not have an <EOL> for the last line in the file, in
    which case it is reset.  Normally you don't have to set or reset this
    voption.  When 'binary' is off the value is not used when writing the
    file.  When 'binary' is on it is used to remember the presence of a
    <EOL> for the last line in the file, so that when you write the file
    the situation from the original file can be kept.  But you can change
    it if you want to.
于 2013-04-25T19:39:54.763 回答
3

在 .vimrc 中有一个简单的偏好,即不在我编辑的每个文件中添加换行符

你可以使用我的PreserveNoEOL 插件。通过这个简单的设置,你就完成了;或者,您也可以影响每个缓冲区:

:let g:PreserveNoEOL = 1
于 2013-04-26T12:11:45.750 回答