Skip to content

Instantly share code, notes, and snippets.

@annhienktuit
Forked from OleksiyRudenko/why-newline.md
Created July 19, 2024 10:53
Show Gist options
  • Save annhienktuit/a0e808d2caed250b7937929dadf5748b to your computer and use it in GitHub Desktop.
Save annhienktuit/a0e808d2caed250b7937929dadf5748b 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.

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