为什么我需要二进制模式的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".
换行符" 或更准确地说是行尾"字符(<EOL>
是终止符行-文件的最后一行实际上是带有<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都有不同的解释-<EOL>
是分隔符行,并且在逻辑上默认为 not 添加<EOL>
在新文件的最后一行的末尾,当遇到<EOL>
时,在真正的最后一行之后添加多余的新行".
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>
-free"状态,请执行: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.
为说明不同的行为,这是当您尝试使用<EOL>
连接两个文件时发生的情况:
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
,这是当您尝试串联两个不带<EOL>
的文件时发生的情况:
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程序默认使用 terminator 解释并在末尾添加<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.
下面的图片显示了一个简单的文件,其中的<EOL>
是用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.
(编辑)该文件中没有第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)
此图显示了另一个不带<EOL>
的简单文件,该文件使用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屋!