Why is Vim adding a newline? Is this a convention?
The convention for Unix text files is that every line is terminated by a newline, and that newlines are line terminators, not line separators.
When Vim saves a buffer as a file, it terminates every line with the end-of-line sequence for that file format, which for Unix is a newline. See
:help 'fileformat'
If you're using Unix text-processing tools, it's best to stick with this convention. However, if you have some need to not put a newline at the end of the last line of a file, you can do so. Vim considers such files to be "binary". See
:help 'binary'
:help edit-binary
Vim 8.0 now provides for this with the fixeol
option. Specifically if you do:
:set nofixeol
then Vim won't add a trailing newline character at the end of the final line if the file didn't already have one.
That could go in a filetype plugin, or possibly even your .vimrc
.
(This is an improvement on :set binary
because it only affects the final line-break character, whereas binary
also changes a bunch of other behaviours, which you probably don't want unless you're actually editing a binary file.)
A newly created file will still have a trailing line-break character by default. You can change that (and switch a file that already has a final newline to not having one) by additionally doing:
:set noeol
That has to be set specifically for each file you wish to change: loading a file into a buffer will always set eol
to match the file's current state.
Vim is not adding anything that you didn't put there yourself.
A "newline" character is not a "new line" and both examples are perfectly normal:
- in the first one, the file only contains one line so you get one "newline" character,
- in the second one, the file contains two lines so you get two "newline" characters.