6

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.

1 Answer 1

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.

19
  • 1
    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
    Commented Dec 24, 2015 at 13:44
  • 2
    Well, you say nonexisting line, I say existing newline character \n that every other editor I have installed will recognise.
    – OJFord
    Commented Dec 24, 2015 at 21:23
  • 1
    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
    Commented Dec 24, 2015 at 21:52
  • 4
    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
    Commented Dec 25, 2015 at 0:01
  • 1
    @OllieFord: This is because of different philosophies of the editors. Notepad et al. interpret NL as "go to another line". Vim, having descended from a line editor, defines a line in the UNIX way: "a line is a string of characters ending with NL". In Vim, there is no line after the last NL. If you want to see the NL itself, :set list. It's like learning to use a fork after years of using a spoon, and complaining that it's not round. Wishing Vim was Notepad is not a productive use of your time.
    – Amadan
    Commented Dec 25, 2015 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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