Vim为什么要在文件末尾添加新行? [英] Why would Vim add a new line at the end of a file?

查看:169
本文介绍了Vim为什么要在文件末尾添加新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用wordpress,有时会临时更改wordpress核心文件,以便了解发生的情况,尤其是在调试时.今天我有点惊讶.当我准备将更改提交到git存储库时,我注意到git status正在将wordpress的文件之一标记为未提交.我记得我在关闭文件之前已将对该文件所做的所有更改还原到该文件中,因此我决定使用diff来查看已更改的内容.我将项目中的文件与保存在下载目录中的wordpress副本中的文件进行了比较.事实证明文件在最后是不同的. diff表示原始文件末尾缺少换行符:

I work with wordpress a lot, and sometimes I changed wordpress core files temporarily in order to understand what is going on, especially when debugging. Today I got a little surprise. When I was ready to commit my changes to my git repository, I noticed that git status was marking one of wordpress' files as not staged for commit. I remember I had reverted all the changes I did to that file before closing it, so I decided to use diff to see what had changed. I compared the file on my project with the file on the wordpress copy that I keep in my downloads directory. It turns out the files differ at the very end. diff indicates that the there is a newline missing at the end of the original file:

1724c1724
< }
\ No newline at end of file
---
> }

我什至从未碰过那条线.我所做的更改位于大文件中间的某个位置.这使我认为vim在文件末尾添加了换行符.为什么会发生这种情况?

I never even touched that line. The changes I made where somewhere in the middle of a large file. This leads me to think that vim added a newline character at the end of the file. Why would that happen?

推荐答案

在这里看到的所有答案都解决了以下问题:"如何防止Vim在文件末尾添加换行符?",而问题是" Vim为什么要在文件末尾添加新行?".浏览器的搜索引擎将我带到了这里,但找不到该问题的答案.

All the answers I've seen here address the question "how could I prevent Vim from adding a newline character at the end of the file?", while the question was "Why would Vim add a new line at the end of a file?". My browser's search engine brought me here, and I didn't find the answer to that question.

这与POSIX标准如何定义行有关(请参见为什么应文件以换行符结尾?).因此,基本上,一行是:

It is related with how the POSIX standard defines a line (see Why should files end with a newline?). So, basically, a line is:

3.206行
零个或多个非< newline>序列字符以及终止的< newline>字符.
3.206 Line
A sequence of zero or more non- <newline> characters plus a terminating <newline> character.

因此,它们都需要以换行符结尾.这就是Vim总是默认添加换行符的原因(因为根据POSIX,它应该一直存在).

And, therefore, they all need to end with a newline character. That's why Vim always adds a newline by default (because, according to POSIX, it should always be there).

不是唯一执行此操作的编辑器. Gedit

It is not the only editor doing that. Gedit, the default text editor in GNOME, does the same exact thing.

许多其他工具也希望使用换行符.参见例如:

Many other tools also expect that newline character. See for example:

  • How wc expects it.
  • GCC warns about it.

另外,您可能对以下内容感兴趣:在文件结尾.

Also, you may be interested in: Vim show newline at the end of file.

这篇关于Vim为什么要在文件末尾添加新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆