0

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?

Magnus
  • 10,736
  • 5
  • 44
  • 57
  • possible duplicate of [Vim show newline at the end of file](http://stackoverflow.com/questions/15639511/vim-show-newline-at-the-end-of-file) – jheddings Oct 23 '13 at 18:13

3 Answers3

1

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

:setlocal eol?
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • 1
    very confusing that it's called "eol" instead of "eof". Overall a disappointing behavior from vim, but so it is – Magnus Oct 24 '13 at 16:46
  • Well, the missing character is an end-of-line / newline character. Think of it only applying to the last line in a buffer. What you find disappointing is just good style in the Unix mindset. The vi heritage shows here, yes. – Ingo Karkat Oct 24 '13 at 18:55
1

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

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

Christian Brabandt
  • 8,038
  • 1
  • 28
  • 32
1

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
romainl
  • 186,200
  • 21
  • 280
  • 313