6

Possible Duplicate:
What's the point in adding a new line to the end of a file?

What is considered better behaviour: to leave the last letter of the last line to be the last byte of a file, or to press Enter at the end to let there be a lf? Why?

Ivan
  • 17,708

2 Answers2

5

(Copied from my comment on the question, as requested.)

See Why should files end with a newline? - Stack Overflow. The "TL;DR": POSIX says so, and it's pretty logical to have a line end in and end-of-line character.

See the linked Stack Overflow question for practical consequences.

janmoesen
  • 2,710
0

I always put an empty line at the end. I think it looks cleaner, and some programs choke on the last line if there isn't a newline.

Kevin
  • 40,767
  • 2
    An empty line at the end means that the file ends in two newline characters. If the file ends with a single newline character, it means the last line is not empty; the newline terminates that last line. – Gilles 'SO- stop being evil' Nov 03 '11 at 23:32
  • @Gilles If you want to be pedantic about it, then I what I meant was "newline". I just said "blank line" because that's the way it displays in every text editor. – Kevin Nov 03 '11 at 23:51
  • Most unix editors do not display a blank line below a file that ends with a newline. That's not just pedantry, it's how most unix programs work. – Gilles 'SO- stop being evil' Nov 03 '11 at 23:56
  • 1
    Interesting, I didn't know that vim adds a newline at the end of the last line automatically. – Kevin Nov 04 '11 at 00:11
  • @Kevin Just think of lines in a text file as records, then it's pretty logical. Pressing Enter means "begin editing a new record". When written to disk, the items in this list of lines are interspersed with newline characters. (Note that more translation is going on: Characters also must be encoded from the internal representation, for example to UTF-8) – Jo So Sep 23 '14 at 11:15