10

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?

  • 7
    "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
  • 1
  • 1
    No. Vim is not adding a new line. – romainl May 28 '15 at 16:13
11

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.

10

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.

(see wiki page: http://vim.wikia.com/wiki/Do_not_auto-add_a_newline_at_EOF)

In your ~/.vimrc add this line:

set nofixeol

Relaunch vim, now it shouldn't add the newline at the end of the file.

(works only since vim version 7.4.785)

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.