13

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jason Down
  • 21,731
  • 12
  • 83
  • 117
  • It wouldn't make any kind of sense on serverfault. Maybe on superuser. But it seems that the consensus is to tolerate vim questions here because vim is considered an IDE. – innaM Jul 29 '09 at 15:07
  • Ok thanks... I wonder if SuperUser is out of private beta yet... guess I can go check – Jason Down Jul 29 '09 at 15:47
  • It's still in beta, but not in private, but in semi-private beta: http://blog.stackoverflow.com/2009/07/super-user-semi-private-beta-begins/ – innaM Jul 29 '09 at 15:55

4 Answers4

6
:help endofline

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

innaM
  • 47,505
  • 4
  • 67
  • 87
  • 3
    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 Jul 29 '09 at 16:02
  • 1
    I agree I want the line there I just also want to be able to see that it's there. – Keith Smiley Mar 25 '13 at 23:53
3

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
  • 4,157
  • 4
  • 27
  • 20
  • 2
    This seems to always show a $ character. Even if there is no blank line afterwards. – Jason Down Jul 29 '09 at 15:57
  • 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 Jul 29 '09 at 16:01
  • See update 2 comments... reworded the question to be more accurate. – Jason Down Jul 29 '09 at 16:17
  • I confirm that even if "diff" prints a difference between those files, using `:set list` those files seem to be identical. – ROMANIA_engineer Jan 31 '15 at 16:43
3

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
  • 238,783
  • 38
  • 220
  • 352
1

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.

Community
  • 1
  • 1
dlamblin
  • 43,965
  • 20
  • 101
  • 140