5

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.

OJFord
  • 425
  • 2
  • 12

1 Answers1

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.

romainl
  • 40,486
  • 5
  • 85
  • 117
  • 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 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
  • 2
    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
  • 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 Dec 24 '15 at 21:52
  • 3
    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 00: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 Dec 25 '15 at 01:21
  • 1
    @Amadan I absolutely don't wish vim was notepad, just hoped for the option like exists in Atom and Sublime - but your explanation makes sense, I understand now. Thanks. – OJFord Dec 25 '15 at 01:33
  • Agh, I so nearly accepted this answer all these years later. But you know, I think I still agree with myself. Vim displays a 0B file the same as a 1B (\n) file. I don't see that an empty line is any weirder of an interpretation of a \n character than nothing. In fact I think it might be saner: any visible character is displayed differently, in effect, according to whether or not it follows a newline ch. Showing a trailing empty line would be more in keeping with 'line feed', too. I'm not sure I actually want this behaviour any more, but I think I still 'believe in it' in principle. – OJFord Apr 23 '21 at 20:20
  • And another year later, I have to ask: if I wrote an answer that made Vim work how you ask in the question, would you accept it (even though you don't think you want Vim to work in this way any more)? If not, I think that probably means you should accept this answer. If so, then I'll write it! – Rich Oct 13 '22 at 08:33
  • @Rich Yet another year later, yes I certainly would! I wouldn't use it myself, but I'm brought back here by an up-vote to OP - people clearly have the same question, and IME in finding others' questions it's annoying when you get 'XY answers' as it were, or solutions that attack it via some aspect not relevant in your case or what the question was actually originally about. Romainl's explanation being most up-voted, but an accepted answer that does actually offer what was requested seems entirely appropriate to me. – OJFord Aug 01 '23 at 14:08
  • 1
    @romainl said:

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

    This is a personal opinion, and one I don't share.

    What is super misleading is the current situation where Vim displays a file that ends with a \n exactly the same as a file that doesn't end in \n. Displaying 2 files with different content the same way is misleading (fact) and wrong (personal opinion :D).

    – Cristian Vasile Mocanu Jan 04 '24 at 20:44
  • @Rich: I would very much like to know how to configure VIM to make it very prominent the difference between a file ends in a \n vs. a file that doesn't. I'm using neovim BTW, if that makes any difference. – Cristian Vasile Mocanu Jan 04 '24 at 20:45
  • @Amadan: "Wishing Vim was Notepad is not a productive use of your time."

    I don't understand why this question is met with condescending remarks so often! I personally don't want Vim to be Notepad. I want Vim to display differently files that have different content (ending or not with \n). Is it so crazy for me to want to see clearly when 2 files are different? Now it just so happens that Notepad (and almost every other text editor in existence) show this difference by showing an empty last line when the file ends with \n. It's a good solution to the above problem.

    – Cristian Vasile Mocanu Jan 04 '24 at 22:13
  • @CristianVasileMocanu Not condescension, reality. Your penultimate line shows that you do in fact prefer the Notepad behaviour. Notepad descends from the philosophy that a newline is a line separator, as romainl explained. Vim comes from a philosophy where a text file not ending in a newline is anomalous, as the last line does not conform to the definition of a line. I can only repeat what I said: if you just want to see where the newlines are, use :set list; otherwise you implicitly agree to the UNIX line definition, and (with set eol) for Vim to correct your badly constructed text file. – Amadan Jan 05 '24 at 01:42
  • @Amadan 'list' doesn't work like that: it displays the configured lcs-eol regardless of whether it's going to write out an <EOL> after the last line when writing the file. – Rich Jan 05 '24 at 10:27
  • @Amadan Writing "Wishing Vim was Notepad" when the person you're talking to clearly isn't doing that does come across as condescending, even if you apparently didn't intend it to. There are one or two other ways in which Notepad differs from Vim, even if they prefer its behaviour in this one area. It's perfectly possible to use Vim without "agreeing to the UNIX line definition": that's presumably why the 'fixendofline' option was added. – Rich Jan 05 '24 at 10:39
  • Just to make it clear: As a long-time VIM user, I don't want VIM to be Notepad. I want to be able to see if the file ends with a \n or not, without being forced to see \n characters on every line. Notepad can do this, so it's far superior to VIM in this respect. Another way is how GitHub does it - to show a Ø at the end of the file, when the file is missing a \n there. Either way works with me (or any other way). The current behavior of VIM (to show different files identically) is just not acceptable. – Cristian Vasile Mocanu Jan 07 '24 at 10:18