Why would Vim add a new line at the end of a file?

僤鯓⒐⒋嵵緔 提交于 2019-11-26 16:12:14

问题


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
---
> }

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?


回答1:


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.

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 Line
A sequence of zero or more non- <newline> characters plus a terminating <newline> character.

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).

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


Edit

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.




回答2:


Because vim is a text editor, it can sometimes "clean up" files for you. See http://vimhelp.appspot.com/vim_faq.txt.html#faq-5.4 for details on how to write without the ending newline, paraphrased below:

How do I write a file without the line feed (EOL) at the end of the file?

You can turn off the eol option and turn on the binary option to write a file without the EOL at the end of the file:
   :set binary
   :set noeol
   :w

Alternatively, you can use:
   :set noeol
   :w ++bin




回答3:


Adding a newline is the default behavior for Vim. If you don't need it, then use this solution: VIM Disable Automatic Newline At End Of File

To disable, add this to your .vimrc

set fileformats+=dos



回答4:


You can put the following line into your .vimrc

autocmd FileType php setlocal noeol binary

Which should do the trick, but actually your approach is somewhat wrong. First of all php won't mind that ending at all and secondly if you don't want to save your changes don't press u or worse manually try to recreate the state of the file, but just quit without saving q!. If you left the editor and saved for some reason, try git checkout <file>




回答5:


3.206 Line A sequence of zero or more non- characters plus a terminating character.

Interestingly, vim will allow you to open a new file, write the file, and the file will be zero bytes. If you open a new file and append a line using "o" then write the file it will be two characters long. If you open said file back up and delete the second line 'dd' and write the file it will be one byte long. Open the file back up and delete the only line remaining and write the file it will be zero bytes. So vim will let you write a zero byte file only as long as it is completely empty. Seems to defy the posix definition above. I guess...



来源:https://stackoverflow.com/questions/14171254/why-would-vim-add-a-new-line-at-the-end-of-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!