Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you force Vim to show a blank line at the end of a file?

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.

like image 564
Jason Down Avatar asked Jul 29 '09 14:07

Jason Down


People also ask

How do I create a blank line in Vim?

Starting in normal mode, you can press O to insert a blank line before the current line, or o to insert one after. O and o ("open") also switch to insert mode so you can start typing. To add 10 empty lines below the cursor in normal mode, type 10o<Esc> or to add them above the cursor type 10O<Esc> .

Does Vim add newline at end of file?

That's why Vim always adds a newline by default (because, according to POSIX, it should always be there). It is not the only editor doing that. Gedit, the default text editor in GNOME, does the same exact thing.

How do I insert a blank line in vi editor?

Opening a blank line: Type o (lower case) to insert, or open, a blank line below the current line. Type O (upper case) to insert a blank line above the current line. Press ESC to get back to command mode.

Which character is displayed after the end of the file in Vim?

The end-of-line character ( lcs-eol , $ by default) is displayed at the end of file (EOF), no matter if the file actually ends with a newline. Neither disabling the fixeol setting ( :set nofixeol ), nor starting Vim in binary mode ( vim --clean -b bar ) affects this behavior.


2 Answers

:help endofline

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

like image 194
innaM Avatar answered Oct 04 '22 16:10

innaM


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.

like image 33
glenn jackman Avatar answered Oct 04 '22 17:10

glenn jackman