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.
- 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.
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.
Missing EOF newlines lead to noisy diffs:
- 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.
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.
Most modern editors support adding a final newline. Just turn it on once and forget about it.
Go to:
Settings → Editor → General → Ensure every saved file ends with a line break
✅ Check it.
Go to Settings → search for:
Files: Insert Final Newline
✅ Enable it.
Or in settings.json
:
"files.insertFinalNewline": true
In Preferences → Settings
:
"ensure_newline_at_eof_on_save": true
Add this to your .vimrc
or init.vim
:
autocmd BufWritePre * if &ft != 'gitcommit' | silent! %s/\%$//e | endif
set fixendofline
Add to your config:
(setq require-final-newline t)
Look for the ⛔ “No newline at end of file” icon in the diff.
Use xxd
, cat -e
, or od
:
cat -e filename
If the final line does not end with $
, it’s missing a newline.
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.
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.