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?
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.
(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)
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.
xxd
to verify, but it still adds a linefeed (0a
) even in binary mode.
Commented
Oct 5, 2016 at 22:54
?>
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.