2

Some people prefer not to end a file with a new line.

I'm not one of them. In fact, I want to see that it's there!

How can I keep this 'force new line at end' behaviour, but show it in the editor? I often leave a double new line accidentally, because I've added one myself too.

4

Like so many people, you seem to mix up "new line" and "newline".

When editing a file where the last line doesn't end with a "newline" character, Vim will add that "newline" character on write unless the 'noeol' and 'binary' option are set. So, basically, you probably always have that "newline" character at the end of your files, whether you see it or not.

The "newline" character can be interpreted in two ways:

  • as a "line terminator", the editor considers anything that comes after a "newline" character to be on another line;
  • as a "line separator", the editor considers that the "newline" character must be between two lines.

In the first interpretation, that "newline" character at the end of the last line can't really be displayed in any useful way because it is the same as any other "newline" character at the end of any other line in the file. If you think about it, there's no real need to even display it.

In the second interpretation, that "newline" character at the end of the last line is not displayed either but, because the interpretation mandates the presence of another line after the "newline" character, the editor adds a "new line" at the end of the file.

Adding such a "new line" at the end of the file just as misleading as it is unnecessary and wrong.

The Vim way is simply to assume the "newline" character is there and thus to not add an unnecessary "new line" at the end of the file.

  • Yes, I know it's there. As I stated I want to see it there. I have other editors configured to display a blank line at the end of a file, I want vim to too. It allows easy appending - Go will start me writing on a new line with a blank between it and the last thing; on save I'll get that blank back. Without a blank line displayed, that's Go<kbd>return</kbd> to achieve the same. Not a massive deal, but it's how I like it even if you disapprove :) – OJFord Dec 24 '15 at 13:44
  • Well no, you can't convince Vim to display a non-existing line. Thankfully. – romainl Dec 24 '15 at 21:19
  • Well, you say nonexisting line, I say existing newline character \n that every other editor I have installed will recognise. – OJFord Dec 24 '15 at 21:23
  • Ending your file with a "newline" character and ending the file with an empty line are two different things. The former is generally considered a good thing that Vim does automatically (unless you tell it otherwise) but there's no built-in way to force it to do the latter without some kind of scripting. Something like an autocommand with the BufWritePre event… – romainl Dec 24 '15 at 21:52
  • But it comes back to what you said about interpretation. The same file opened in e.g. Notepad, Atom, TextEdit, Sublime has an empty line displayed at the end. – OJFord Dec 25 '15 at 0:01

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.