16

Vim normally writes <EOL> for the last line only when there was one in the original file.

How to make vim to write a new line at the end of a file all the time (when the file is not binary and there isn't already one)?

I've tested eol option, but it didn't work as expected.

See also: Preserve missing end-of-line at end of text files, but opposite. And: Why should files end with a newline?

17

Basic info

Vim doesn't show latest newline in the buffer but actually vim always place EOL at the end of the file when you write it, because it standard for text files in Unix systems. You can find more information about this here. In short you don't have to worry about the absence a new lines at the end of the file in vim.

Experiment 1

You can do this small experiment to check this behaviour. In the terminal execute:

$vim test_test

enter for example only character 1 and save file :w.

You have only one character in the file, right? Actually no :) You now have in your file 1 and LF characters. Let's check it, with hexdump utility:

$hexdump test_test

output:

0000000 31 0a
0000002

31 is ASCII code for character 1, and 0a is ASCII code for non-printing LF character which for the UNIX as you may already know code for end of line.

Experiment 2

But what if we will open file which don't have eol symbol at the end of the file. Let's check it too. Let's create file without eol at the end, for example like this:

$echo -n "1" > test_without_eol

and then again let's look inside it with hexdump

$hexdup test_without_eol

output will be:

000000 31
0000001

So there are no any eol characters here. Let's open this file in vim:

$vim test_without_eol

At the bottom of editor you'll see:

"test_without_eol" [noeol] 1L, 1C

And if you'll save this file eol character will be putted automatically.

What we can do about this?

If you really need to see end of the file. Try to use this command:

:set listchars=eol:$

it will show all eol characters as $ sign.

  • Great. I came to the same conclusion after my confusion, because Vim wasn't actually showing me this new line, but it was there after save. – kenorb May 16 '15 at 11:33
  • 1
    @kenorb by the way you can show all EOL charachters using this comand :set listchars=eol:$ – Alexander Myshov May 16 '15 at 11:41
  • "wasn't actually showing me this new line" - in Unix, there is no line. A line is defined as "number of non-LF characters, terminating in a LF character". If you think of LF not as "go to next line" but "end of line", what Vim does suddenly makes much more sense. – Amadan Mar 10 '16 at 4:56
8

Vim only omits the EOL if the file is opened as 'binary' and the 'endofline' option is reset (when the binary file didn't have an EOL when opening, or you explicitly reset the option). In other words, Vim only honors a missing EOL for binary files. For text files, it always writes with an (added) EOL. (That's why you need solutions like my PreserveNoEOL plugin to keep text files without EOL.) You can find all the details at :help 'endofline'.

So, you only need to ensure to open the file as non-binary in order to have Vim automatically write an EOL.

5

As Alexander mentioned, Vim doesn't show EOL at EOF, that's why it's so confusing (especially with empty/blank new line which is another thing), therefore it actually writes every-time on file save.

Here is simple test how to test if that works:

$ printf foo > foo.txt
$ cat foo.txt
foo$ wc foo.txt
0 1 3 foo.txt
$ vim -cwq foo.txt
$ cat foo.txt
foo
$ wc foo.txt
1 1 4 foo.txt

Notice how shell prompt behave when there is no EOL (it goes mixed with the next line). When re-saving file with Vim, it adds it.

Using wc we can count lines, words and characters before saving and after, so we can see that it actually changes.

Why this is so important? To keep our files POSIX compliant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.