I am trying to setup vim to skip adding eol on last line or eof, i have tried this
:set binary :set noeol :w
which is not perfect cause binary override filetype for later use.
Any other option to set this, i don't need newline on last line.
EOF is not same of null character \0 . I think you want to know whether you can get an EOF without getting a newline first. The answer is yes. It is possible if there is no newline at the end of file, i.e. when the last line finished without a newline at end.
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).
I wanted to add an answer that I think can be as useful. The selected answer always remove the EOL on files even if they had one to begin with. This may be the behavior that you want, it also may not be. In my opinion I want to preserve the EOL as I originally opened the file.
What I suggest is a slight modification. Put set binary
at the top of your .vimrc
file. This way any files will open in binary mode. If they have no EOL then vim will detect this and leave it NOEOL. If they have an EOL then it'll recognize it as having an EOL and leave it be.
If you would like new files to also not have EOL then you should set,
au BufNewFile * set noeol
The commands for the chap would be if you always want NOEOL ever. The one thing to be aware of is that if you have a file with spaces at the bottom, then you will lose spaces at the end. This is because the following happens,
This process will repeat until the EOL's are gone and the file ends in a single character. With my setup, what happens is the file is opened and if VIM sees at least one EOL then it keeps it internally. If you save that file, no matter what it is there (you can check by typing set eol?
. In the case that you want to get rid of that internally stored EOL you just say set noeol
and then save and BOOM, the last EOL is removed.
WHOOO, I am winded typing this.
This is even better i found somewhere:
au BufWritePre * :set binary | set noeol au BufWritePost * :set nobinary | set eol
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With