newlines at the ends of files?

i went on a formatting spree today since some of my site’s files are only somewhat formatted, and found out that prettier has a habit of adding a newline at the end of files (that can’t be turned off).

i am honestly really not a fan of this, but apparently the general consensus is that it’s necessary? it makes git diffs less weird (if you add something to the end of a file and there’s no newline, the previous ending line is shown as “deleted” rather than changed). it also displays nicer when printed inside a terminal, since there may be text displayed after the code

i really do not care about either of these things, but i care about consistency so i have begrudgingly formatted all my files (including my html!) to end on a newline.

what are your guys’ thoughts on trailing newlines? (and do you use a code formatter?)

I don’t insist on it, or go out of my way to add a trailing newline, but a text file must always end in one according to the Posix standard, so it’s generally a good idea. Programming text editors can also be configured to add a trailing newline if there isn’t one already, so that’s easy.

1 Like

I think it’s a UNIX thing, and I’m OK with it. An empty line at the end of the file is as good an EOF marker as any.