# Don't add new line at the end of a file

Vim always adds a new line at the end of my files when saving, and it is causing errors in my PHP scripts.

How can I make vim not add this new line on save?

• "it is causing errors in my PHP scripts" -> This is strange? PHP should work fine with a trailing newline. In fact, this is pretty much standard. Perhaps you have a blank line after the ?> which PHP outputs to the browser (causing the "headers already sent"-error)? You don't need a trailing ?>, and it is usually recommended to leave it out to prevent exactly this problem. – Martin Tournoij May 28 '15 at 15:53
• I guess thats misleading. Our scripts interpret any output as an error. – Dan May 28 '15 at 15:55
• No. Vim is not adding a new line. – romainl May 28 '15 at 16:13

This isn't a full empty line, just a final newline at the end of the last line. Unix tools (like Vim's heritage) insist on adding that, whereas the Windows operating system is not so strict. You can read more on that at Why should files end with a newline?

Unfortunately, it's quite complex to prevent Vim from writing the final newline. My PreserveNoEOL plugin can do this. For a discussion of approaches, also see VIM Disable Automatic Newline At End Of File.

In new versions of Vim there's finally an option for this

Vim 7.4.785 adds the 'fixeol' option that can be disabled to automatically preserve any missing EOL at the end of the file.

In your ~/.vimrc add this line:
set nofixeol

Relaunch vim, now it shouldn't add the newline at the end of the file.
Have you tried opening the file in binary mode? Try vim -b file_name. It should not add the new line at end of file.
• I tried doing this, and then using xxd to verify, but it still adds a linefeed (0a) even in binary mode. – DJMcMayhem Oct 5 '16 at 22:54