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?

0

2 Answers 2

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.

1

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.

5
  • 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. Nov 3, 2011 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 3, 2011 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. Nov 3, 2011 at 23:56
  • Interesting, I didn't know that vim adds a newline at the end of the last line automatically.
    – Kevin
    Nov 4, 2011 at 0: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, 2014 at 11:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.