When one opens a file with an incomplete last line (File does not end with 0x0A
/ a new line character), the endofline
option is set to false (:echo &endofline
will print 0
) and the [NOEOL]
indicator is displayed.
If one saves such a file (:w
), the new line character will be added, the [NOEOL]
indicator will disappear and the filesize will grow by one byte. However, :echo &endofline
will still print 0
. I would expect the endofline
option being in sync with the [NOEOL]
indicator.
Is there a way to keep the [NOEOL]
indicator in sync with the endofline
option? Or alternatively to query the status of the [NOEOL]
indicator?
Thanks to @filbranden's answer I added the following line to my .vimrc
file:
au BufWritePost * if !&endofline && &fixeol && !&binary | set endofline | endif
The endofline
option will now be updated if a new line character was added to a file's end. However, this is only half the way: If I remove a trailing new line character from a file, the endofline
option will stay true
.
FYI: I need this to edit some binary files (:set binary | %!xxd
/ %!xxd -r
) without corruption through accidental added/removed trailing new line characters.
:%!
operation... @D.BenKnoble you wrote vim-hex (which you @dirdi might want to try!), can you pitch in on adding/removing newlines when xxd'ing back and forth?:%!
operation. Vim adds a trailing new line char to the stream if theendofline
option is set. But theendofline
option is not in sync with the buffer's content. AFAICSvim-hex
is also affected by this problem.