Skip to content

Instantly share code, notes, and snippets.

@OleksiyRudenko
Last active October 4, 2024 16:39
Show Gist options
  • Save OleksiyRudenko/d51388345ea55767b7672307fe35adf3 to your computer and use it in GitHub Desktop.
Save OleksiyRudenko/d51388345ea55767b7672307fe35adf3 to your computer and use it in GitHub Desktop.
Why should text files end with a newline?

Why should text files end with a newline?

Reasons:

  • UNIX standard
  • If the last line in a file doesn't end with a newline then addition of next line affects two lines instead of one. This also pollutes diff on multiple files, so reader may wonder what has changed in a line whereas no significant change has occured.

Multiple newlines at the file end are also redundant as well as spaces at the end of line.

How to fix properly and once

Ensure your IDE/editor has the relevant setting set:

  • IntelliJ: navigate to Settings > Editor > General and
    • check Ensure line feed at file end on Save
    • set Strip trailing spaces on Save to Modified Lines
  • VSC: navigate to Preferences > Settings and
    • search for "newline" and check the following
      • Files: Insert Final Newline
      • Files: Trim Final Newlines
    • search for "whitespace" and check Trim Trailing Whitespace
  • Sublime Text: navigate to Preferences > Settings > Default find and set the following to true:
    • trim_trailing_white_space_on_save
    • ensure_newline_at_eof_on_save
    • ... or use Single Trailing Newline package to ensure one and only one newline at the end of file

Other IDEs may have a setting similar to any of the above.

@Arcitec
Copy link

Arcitec commented Oct 2, 2024

Can also be achieved with:

https://spec.editorconfig.org/index.html

insert_final_newline = true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment