views:

599

answers:

4

When I open a text file in Notepad, it shows a blank line if there is a carriage return at the end of the last line containing text. However, in Vim it does not show this blank line. Another thing I've noticed is that the Vim editor adds a carriage return to the last line by default (even though it doesn't show it). I can tell, because if I open a file in Notepad that was created in Vim, it shows a blank line at the end of the file.

Anyway, I can live with these two differences, but I'm wondering if there is an option in Vim that allows you to toggle this behaviour.

Thanks

PS - GVim 7.2

[Update]

Would this make sense to be on Server Fault instead?

[Update 2]

I'll rephrase this... I need to know when there is a carriage return at the end of single line file (Notepad shows an extra line with no text, with Vim I cannot tell). This is due to a Progress program that reads a text file (expects a single line, but with a carriage return) and parses the text for some purpose. If there is no carriage return, Progress treats the line as if it is null.

[Workaround Solution] One way I've found to ensure there is a carriage return (but make sure I don't add a second one) is to make sure I have the end of line write option turned on (:set eol) and then just do a write/save. This will put an end of line in the file if it's not already there. Otherwise, it doesn't add a new one.

+2  A: 
:help endofline

explains how you could stop vim from adding an extra newline.

innaM
Solves one problem... it's the other one (not showing that there is a blank line) that is the bigger issue for me though =\ Has to do with needing to ensure there is a carriage return at the end of a line (and only one).
Jason Down
+1  A: 

One useful Vim option is

set list

It will help you see all end of lines characters (and possibly other generally invisible chars). So you will be able to view this last endofline directly in Vim and not only in Notepad.

skinp
This seems to always show a $ character. Even if there is no blank line afterwards.
Jason Down
There is no blank line afterwards. vim considers a line something that has a newline at its end. The blank line you are talking about doesn't have a newline, so vim concludes that there is nothing to display.
innaM
See update 2 comments... reworded the question to be more accurate.
Jason Down
A: 

When you open the file in VIM the status line should say [noeol] after the filename. So that's one indication. As Manni said, you can change this by setting both the endofline option off and the binary option on. You can set this as your default settings in a .vimrc file.

dlamblin
A: 

It seems that vim treats newline as a line terminator, while notepad treats it as a line separator: from http://en.wikipedia.org/wiki/Newline

There is also some confusion whether newlines terminate or separate lines. If a newline is considered a separator, there will be no newline after the last line of a file. The general convention on most systems is to add a newline even after the last line, i.e., to treat newline as a line terminator. Some programs have problems processing the last line of a file if it isn't newline terminated. Conversely, programs that expect newline to be used as a separator will interpret a final newline as starting a new (empty) line. This can result in a different line count being reported for the file, but is otherwise generally harmless.

If I recall correctly, on unix-y systems a text file must be terminated with a newline.

glenn jackman