How do I prevent Vim from adding a newline at the end of a file when saving?
Answer
:set nofixendofline
Explanation
By default, Vim enforces POSIX compliance by appending a final newline to any file that lacks one. The fixendofline option (enabled by default) controls this behavior. Setting :set nofixendofline disables it, so Vim writes the file exactly as its buffer represents it — no newline added.
This matters when editing files that must not gain a trailing newline: certain binary protocols, embedded data files, legacy formats, or repos with strict no-newline-at-end lint rules.
How it works
fixendofline(default on) — if the buffer lacks a final newline, Vim adds one at write time:set nofixendofline— disables this; the file is written byte-for-byte as buffered- You can also toggle it with
:set noeol(the older alias) or check the current state with:set fixendofline?
Example
A file ends with data and no trailing newline. To keep it that way:
:set nofixendofline
:write
With the option off, :write no longer silently appends \n. You can verify with :set eol? — a value of noeol confirms the buffer has no final newline.
Tips
- Put
set nofixendoflinein an autocmd for specific file extensions:autocmd BufRead *.bin set nofixendofline - Pair with
:set binaryto open binary files without Vim's text-mode transformations - In Neovim, you can check
vim.bo.fixendoflinefrom Lua - The opposite — ensuring a final newline — is achieved with
:set fixendofline(the default)