sponsor Vim development Vim logo Vim Book Ad

PreserveNoEOL : Preserve missing EOL at the end of text files.

 script karma  Rating 71/22, Downloaded by 2387  Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
script type
This plugin causes Vim to omit the final newline (<EOL>) at the end of a
text file when you save it, if it was missing when the file was read. If the
file was read with <EOL> at the end, it will be saved with one. If it was read
without one, it will be saved without one.

Some (arguably broken) Windows applications (also several text editors) create
files without a final <EOL>, so if you have to interoperate with those, or
want to keep your commits to revision control clean of those changes, this
plugin is for you.

This works for all three line ending styles which Vim recognizes: DOS
(Windows), Unix, and traditional Mac. Multiple strategies are implemented to
handle these cases, so you can choose the one that fits you best.

Except for the internal Vimscript implementation, all other strategies first
let Vim save the file as usual (with a final <EOL>), and then post-process (on
BufWritePost) the file contents, using file-system API functions to truncate
the final <EOL>.

The pure Vimscript implementation is based on the following VimTip:

If you always want to preserve a misssing <EOL> in text files, just put
    :let g:PreserveNoEOL = 1
into your vimrc and you're done. If you need more fine-grained control or
want to just turn this on in particular situations, you can use the following
commands or the buffer-local flag b:PreserveNoEOL.

:PreserveNoEOL          For the current buffer, the 'noeol' setting will be
                        preserved on writes. (Normally, Vim only does this for
                        'binary' files.) This has the same effect as setting
                        the marker buffer variable:
                            let b:PreserveNoEOL = 1

:SetNoEOL               When writing the current buffer, do not append an
                        <EOL> at the end of the last line, even when there
                        used to be one. Same as
                            setlocal noeol | let b:PreserveNoEOL = 1
install details
This script is packaged as a vimball. If you have the "gunzip" decompressor
in your PATH, simply edit the *.vmb.gz package in Vim; otherwise, decompress
the archive first, e.g. using WinZip. Inside Vim, install by sourcing the
vimball or via the :UseVimball command.
    vim PreserveNoEOL*.vmb.gz
    :so %
On Linux / Unix systems, you also have to make the "noeol" script executable:
    :! chmod +x ~/.vim/noeol

To uninstall, use the :RmVimball command.

- Requires Vim 7.0 or higher.
- Vim with the Python (2.x) interface or the Perl interface (optional)
- System Perl interpreter (optional)

For a permanent configuration, put the following commands into your vimrc:

This plugin supports multiple strategies for keeping the <EOL> off of text
When Vim is compiled with +python support, a Python function is used to
strip off the trailing newline after writing the buffer. This even works with
'readonly' files.
    let g:PreserveNoEOL_Function = function('PreserveNoEOL#Python#Preserve')
When Vim is compiled with +perl support, a Perl function is used to strip
off the trailing newline after writing the buffer. This even works with
'readonly' files.
    let g:PreserveNoEOL_Function = function('PreserveNoEOL#Perl#Preserve')
Without Perl support, an similar Perl script is invoked as an external
executable. This still requires an installed Perl interpreter, but no Perl
support built into Vim.
    let g:PreserveNoEOL_Function = function('PreserveNoEOL#Executable#Preserve')
As a fallback, a pure Vimscript implementation can be used. This temporarily
sets the 'binary' option on each buffer write and messes with the line
    let g:PreserveNoEOL_Function = function('PreserveNoEOL#Internal#Preserve')

The processing can be delegated to an external executable named "noeol". It is
located in 'runtimepath' or somewhere on PATH. On Windows, this Perl script is
invoked through the Perl interpreter. You can use a different path or
executable via:
    let g:PreserveNoEOL_Command = 'path/to/executable'

You can influence the write behavior via the buffer-local variable
b:PreserveNoEOL. When this evaluates to true, a 'noeol' setting will be
preserved on writes.
You can use this variable in autocmds, filetype plugins or a local vimrc to
change the behavior for certain file types or files in a particular location.

If you want to indicate (e.g. in your 'statusline') that the current file's
missing EOL will be preserved, you can use the PreserveNoEOL#Info#IsPreserve()
function, which returns 1 if the plugin will preserve it; 0 otherwise.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
PreserveNoEOL-1.01.vmb.gz 1.01 2013-04-26 7.0 Ingo Karkat In the Python strategy, support traditional Mac (CR) line endings, too.
PreserveNoEOL-1.00.vmb.gz 1.00 2013-04-26 7.0 Ingo Karkat Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
Vim at Github