Question

How I can see in vim when a file has a newline character at the end? It seems vim always shows one, whether it's truly there or not.

For example, opening a file that has a newline at the end:

echo "hi" > hi
# confirm that there are 3 characters, because of the extra newline added by echo
wc -c hi 
# open in binary mode to prevent vim from adding its own newline
vim -b hi
:set list

This shows:

hi$

Now by comparison, a file without the newline:

# prevent echo from adding newline
echo -n "hi" > hi
# confirm that there are only 2 characters now
wc -c hi 
# open in binary mode to prevent vim from adding its own newline
vim -b hi
:set list

Still shows:

hi$

So how can I see whether a file truly has a newline at the end or not in vim?

Was it helpful?

Solution

Vim stores this information in the 'endofline' buffer option. You can check with

:setlocal eol?

OTHER TIPS

In the second case, Vim displays [noeol] when loading the file.

Don't you see the [noeol] output, when loading that file?

Vim warns you when it opens/writes a file with no <EOL> at the end of the last line:

"filename" [noeol] 4L, 38C
"filename" [noeol] 6L, 67C written
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top