为什么我需要二进制模式的 vim 才能让“noeol"工作? [英] Why do I need vim in binary mode for 'noeol' to work?
问题描述
这个问题是对解决的后续问题,我用于在vim中保存文件而无需换行符文件末尾强行添加"的烦恼.
This question is a follow up to the Work Around I use for "saving files in vim without having the newline at end of file forcibly added" annoyance.
基本上我不能在我的 .vimrc
中使用 set noeol
因为它什么都不做!
Basically I can't use set noeol
in my .vimrc
because it does Nothing!
如果我以二进制模式编辑文件,它会做它应该做的事情.(vim -b file
而不是 vim file
)
It does what it's supposed to do though if I edit the file in binary mode. (vim -b file
instead of vim file
)
这是为什么?
无论如何在 .vimrc
中有一个简单的首选项,不要在我编辑的每个文件中添加换行符?
Anyway to have a simple preference in .vimrc
to NOT add newlines at every single file I edit?
另外,如果我开始以二进制模式编辑每个文件,我会遇到什么样的问题?到目前为止,我还没有看到任何区别.
Also, what kind of issues will I encounter if I start to edit every file in binary mode? So far I haven't seen any difference.
推荐答案
Vim 在文件最后一行末尾添加"的是换行"字符,不要与换行"混淆".
What Vim "adds" to the end of the last line in your file is the "newline" character, which should not to be confused with a "new line".
newline"字符或更准确的行尾"字符(<EOL>
) 的意思是在这一点之后的任何内容都必须被视为在另一行上".根据这种解释——
是一行 终止符——文件的最后一行实际上是带有
.
The "newline" character or more accurately "end of line" character (<EOL>
) means "whatever comes after this point must be considered to be on another line". With this interpretation — <EOL>
is a line terminator — the last line of the file is effectively the last one with an <EOL>
.
问题在于大多数编辑器和 IDE 有不同的解释——
是一行 分隔符——并且,逻辑上,默认为 not 在新文件的最后一行末尾添加一个
,当遇到
时,添加一个多余的newline" 在真正的最后一行之后.
The problem is that most editors and IDEs have a different interpretation — <EOL>
is a line separator — and, logically, default to not add an <EOL>
at the end of the last line of a new file and, when they encounter an <EOL>
, add a superfluous "new line" after the real last line.
简而言之,Vim 不会添加新行":其他编辑器(错误地)将其新行"解释为新行".
In short, Vim doesn't add a "new line": other editors interpret (wrongly) its "newline" as a "new line".
但是您可以通过执行以下操作来解决该问题:在编写文件之前,如果您希望它保持<EOL>
:set binary noeol>-免费".
But you can get around that issue by doing the following: before you write your file, do :set binary noeol
if you want it to stay "<EOL>
-free".
然而,:h 'binary'
关于 :set binary
的危险有很多话要说,所以我会说一直打开"它听起来是个坏主意.
However, :h 'binary'
has a lot to say about the perils of :set binary
so I'd say that turning it "on" all the time sounds like a bad idea.
为了说明不同的行为,当您尝试使用
连接两个文件时会发生以下情况:
To illustrate the different behaviors, this is what happens when you try to concatenate two files with <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
这就是当您尝试在没有
的情况下连接两个文件时发生的情况:
and this is what happens when you try to concatenate two files without <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> 的原因.
最后一行末尾的字符.
The first behavior is somehow the expected behavior and the reason why Vim and many (if not most) UNIX-y programs default to the terminator interpretation and to adding an <EOL>
character at the end of the last line.
下图显示了一个用nano
创建的带有
的简单文件(与Vim相同)并在Eclipse、TextMate、Sublime Text中打开、Vim、Xcode 和 TextEdit.
The picture below shows a simple file with <EOL>
created with nano
(it would be the same with Vim) and opened in Eclipse, TextMate, Sublime Text, Vim, Xcode and TextEdit.
">
(edit) 这个文件中没有第 4 行,唯一能正确显示文件的编辑器是 Vim.行号列的唯一目的是提供有关缓冲区的信息.显示只有 3 行的 4 行是一个严重错误.(结束编辑)
(edit) There is no line 4 in this file and the only editor of the bunch that displays the file correctly is Vim. A line number column's only purpose is to provide information on the buffer. Showing 4 lines where there are only 3 is a gross mistake. (endedit)
此图显示了另一个没有
的简单文件,使用 Sublime Text 创建并在相同的编辑器/IDE 中打开.
This picture shows another simple file without <EOL>
created with Sublime Text and opened in the same editors/IDEs.
这篇关于为什么我需要二进制模式的 vim 才能让“noeol"工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!