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

27,796

Solution 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:

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

Solution 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

Solution 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

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

Share:
27,796

Related videos on Youtube

Buzu
Author by

Buzu

Updated on July 09, 2022

Comments

  • Buzu
    Buzu 11 months

    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?

    • Peque
      Peque almost 8 years
      This is not a duplicate of VIM Disable Automatic Newline At End Of File. This question is asking about the reason: "why is Vim doing that?".
    • Buzu
      Buzu almost 8 years
      Thank you Peque. It's the little details in the phrasing of sentences that make all the difference.
  • Max
    Max over 10 years
    That git solution there also looks very interesting.
  • Buzu
    Buzu over 10 years
    Thanks, I was unaware that it was the default behavior for Vim.
  • Buzu
    Buzu over 10 years
    using q! would not work, because at some point I already saved changes to that file. Otherwise I cannot see the result of the test I'm performing. I already knew how to recover the file, but did not know why Vim would add a newline character. Thanks for the line to add to the vimrc file.
  • Buzu
    Buzu over 10 years
    Thanks for the answer, and for that link. It seems to be a very useful collection of FAQs.
  • Andrew Ferrier
    Andrew Ferrier almost 10 years
    This doesn't work for me. I have fileformats set to unix,dos,mac (I'm on OS X), yet vim still add EOF lineendings as the OP describes.
  • Sergio Abreu
    Sergio Abreu over 6 years
    didn't work here - linux ubuntu. I had to use set noeol binary as Max said below.
  • Sergio Abreu
    Sergio Abreu over 6 years
    Sometime one wants an editor that obeys exactly what the user has typed, and in the case he didn't enter a new line, so... this standard can produce shit if that new line was not expected. It is very specific cases, but I prefer to have new line only when I type it.
  • Myles Prather
    Myles Prather over 2 years
    I learned the very hard way (2 hours of debug) that tcsh won't execute the last line of a script if it does not have a newline character. I was script-generating the tcsh script and forgot to add a final \n to the file. Then I noticed that if I just opened the file in vim and did nothing else except save it, it would suddenly start working.