Skip to content

Instantly share code, notes, and snippets.

@olivertappin
Last active August 26, 2025 08:15
Show Gist options
  • Select an option

  • Save olivertappin/19a95236d8b69f1c9eadfc799a9ab7fc to your computer and use it in GitHub Desktop.

Select an option

Save olivertappin/19a95236d8b69f1c9eadfc799a9ab7fc to your computer and use it in GitHub Desktop.

Revisions

  1. olivertappin revised this gist Aug 6, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion eof-line.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # 📄 Why You Should Always End Files With a Newline (EOF Line)
    # 📄 Why You Should Always End Files With a Newline

    One of the most common (and often misunderstood) code review comments is:

  2. olivertappin revised this gist Aug 6, 2025. No changes.
  3. olivertappin created this gist Aug 6, 2025.
    136 changes: 136 additions & 0 deletions eof-line.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,136 @@
    # 📄 Why You Should Always End Files With a Newline (EOF Line)

    One of the most common (and often misunderstood) code review comments is:

    > “Please add a newline at the end of the file.”
    In GitHub, you’ll see a little **"⛔ no entry" icon** at the end of a file when this is missing. This Gist explains **why** it matters, **when** it's necessary, and **how** to fix or automate it in your editor or workflow.

    ---

    ## ✅ TL;DR (For the Impatient)

    - POSIX requires a newline at the end of text files.
    - Some tools and compilers will **warn**, **fail**, or behave unexpectedly without it.
    - Git diffs can be misleading or messy without it.
    - Most editors can fix this automatically. Just enable the right setting.

    ---

    ## 🧠 Why Does It Matter?

    ### 1. **POSIX Compliance**
    POSIX defines a text file as:

    > "A sequence of zero or more lines, each ending in a newline character."
    So, technically, a file *without* a newline at the end isn’t a valid text file under POSIX standards. This matters for many Unix tools, compilers, and linters that assume this format.

    ### 2. **Cleaner Diffs in Git**
    Missing EOF newlines lead to noisy diffs:

    ```diff
    - return 0;
    \ No newline at end of file
    + return 0;
    +
    ```

    This makes code reviews harder to read and can introduce confusion. When both files have the newline, the diff is clean.

    ### 3. **Tooling Compatibility**
    Some tools—especially in C/C++/Java environments—can behave unpredictably if the final line isn’t properly terminated. For instance:

    - Linters may fail.
    - Compilers may throw warnings.
    - Unix utilities like `cat`, `grep`, `tail`, etc., _may_ behave inconsistently.

    ---

    ## 🛠️ How to Fix It (Automatically!)

    Most modern editors support adding a final newline. Just turn it on once and forget about it.

    ### JetBrains (IntelliJ, WebStorm, etc.)
    Go to:
    `Settings → Editor → General → Ensure every saved file ends with a line break`
    ✅ Check it.

    ### Visual Studio Code (VSCode)
    Go to **Settings** → search for:
    ```
    Files: Insert Final Newline
    ```
    ✅ Enable it.

    Or in `settings.json`:
    ```json
    "files.insertFinalNewline": true
    ```

    ### Sublime Text
    In `Preferences → Settings`:
    ```json
    "ensure_newline_at_eof_on_save": true
    ```

    ### Vim / Neovim
    Add this to your `.vimrc` or `init.vim`:
    ```vim
    autocmd BufWritePre * if &ft != 'gitcommit' | silent! %s/\%$//e | endif
    set fixendofline
    ```

    ### Emacs
    Add to your config:
    ```lisp
    (setq require-final-newline t)
    ```

    ---

    ## 🧪 How to Check If a File Has an EOF Newline

    ### In GitHub
    Look for the ⛔ “No newline at end of file” icon in the diff.

    ### In Terminal
    Use `xxd`, `cat -e`, or `od`:
    ```bash
    cat -e filename
    ```

    If the final line does **not** end with `$`, it’s missing a newline.

    ---

    ## 🧭 When Should You Enforce This?

    Always, unless:

    - You're editing a **binary** file (e.g., image, compressed data).
    - You're dealing with legacy formats where the newline might break interpretation.

    Otherwise, it’s a best practice for **all code**, **configs**, **scripts**, and **docs**.

    ---

    ## 🗣️ Summary

    Adding a newline at the end of a file:

    - Helps maintain standards.
    - Prevents weird bugs and warnings.
    - Keeps version control diffs clean.
    - Can be automated with a single setting or hook.

    ---

    ## 📚 References & Further Reading

    1. [POSIX Definition of a Text File](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_397)
    2. [Why should text files end with a newline? - Stack Overflow](https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline)
    3. [GitHub: "No newline at end of file" icon explained](https://github.com/git/git/commit/4f3e4c3dbd0b9c1f6f39e4f3175e0cbbdbeb45f1)
    4. [Pre-commit Hook: end-of-file-fixer](https://github.com/pre-commit/pre-commit-hooks#end-of-file-fixer)
    5. [EditorConfig Docs: insert_final_newline](https://editorconfig-specification.readthedocs.io/en/latest/#end-of-line)
    6. [GNU Coding Standards - Final Newline](https://www.gnu.org/prep/standards/html_node/Implementation.html)