43

Using the set eol option Vim automatically adds a newline to the end of the file when it's saved. I have this option on but I would like to be able to see this newline in Vim, so I know that it's actually there.

For example with a file in Vim: Vim screenshot

And the same file in TextMate: TextMate screenshot

This always tricks me and I end up adding a second new line and end up realizing later. This exact same question was posted here but the answer that was accepted didn't answer this portion of the question.

Using set list: List screenshot

I can see there is a $ character denoting a new line after the last line but this also litters the rest of the file with these. I know I could set up a toggle for this but I'd really prefer the TextMate like behavior.

Community
  • 1
  • 1
Keith Smiley
  • 61,481
  • 12
  • 97
  • 110
  • The tidle(`~`) means no new line. Isn't that enough? – Billy Chan Mar 26 '13 at 14:29
  • 4
    @BillyChan I want to see that there _is_ a new line at the end. Which there is in the screenshots, even though there is a tilde on the same line, so that doesn't make much sense? – Keith Smiley Mar 26 '13 at 14:36
  • I myself would find seeing `$` very confusing so I would not bother with that:) See if other guys has good answer for you. – Billy Chan Mar 26 '13 at 14:41
  • 1
    Yea I don't like that either. – Keith Smiley Mar 26 '13 at 14:49
  • 1
    @BillyChan the tilde (`~`) does not mean no new line. If there is a newline at the end of the last line, it would still show a tilde on the next line. (As in the first picture) – Nathan Hinchey Jan 31 '18 at 22:18
  • Alternative solution: show `[noeol]` in the statusline https://vi.stackexchange.com/questions/18238/how-to-show-noeol-flag-in-custom-statusline – Flux Sep 16 '19 at 13:27

1 Answers1

16

'endofline' is on by default so you don't need it in your ~/.vimrc.

EOL or "newline" doesn't mean "there's an empty line after here", it means "this marks the end of the line, any further characters are to be displayed on another line". "newline" != "new line".

The last line of your file is #21 and it ends with a "newline" character. Since there's no actual line after that "newline" character, no line #22, showing a line #22 is not only wrong but misleading.

TextMate's behavior is wrong.

Vim's behavior is correct.

If you want Vim to show a line #22, you'll need to explicitly add that line but it sounds rather silly to me.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • 11
    One thing to note is that this behavior is by every native OS X text editor, not just TextMate – Keith Smiley Mar 26 '13 at 16:19
  • Could you post your file? I don't see that pesky line here on the two latest version of textmate I just downloaded. – romainl Mar 26 '13 at 16:56
  • 3
    go to your last line, press enter, save it, open in vim, that line you just created won't be there. – Keith Smiley Mar 26 '13 at 21:27
  • 12
    Though correct, the problem with this behavior is that, a human reader cannot easily finds out whether there is an eol for the last line of the file, except for that `[noeol]` sign in the bottom status line (which is way too hard to notice). Things get worse with the fact that almost all GUI text editors in Win/Mac adopt the other way: treat newline as line separator rather than line terminator, so that the visual appearance around the end of file directly tells user the existence (or non-existence) of an eol. – neverhoodboy Apr 28 '15 at 02:32
  • Add the `[noeol]` sign will only be there right after you open the file. It will disappear once you perform any non-trivial operation. – neverhoodboy Apr 28 '15 at 03:47
  • 19
    TextMate's behavior isn't wrong at all! Every editor in the world shows that empty line which vim doesn't display! – Iulian Onofrei Nov 15 '16 at 10:36
  • 4
    @KeithSmiley, How did you solve the problem? Why did you accepted this as a solution? – Iulian Onofrei Nov 15 '16 at 10:37
  • @IulianOnofrei, there's no line to display. – romainl Nov 15 '16 at 10:38
  • 4
    @romainl, `vim test` > `i` > `enter` > `esc` > `:wq` > `vim test` > line 2 is gone! I manually pressed the enter key with my fingers and typed it, so I expect it to remain there, as I typed it on purpose, yet it disappears! Vim is wrong. – Iulian Onofrei Nov 15 '16 at 10:41
  • @IulianOnofrei, no, line 2 is not gone. – romainl Nov 15 '16 at 10:44
  • 1
    @romainl, Are you for real? Before `:wq`, the sidebar shows `1 2`, after it, the sidebar shows `1`. Are you trying to tell me that these are equal? – Iulian Onofrei Nov 15 '16 at 10:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128153/discussion-between-iulian-onofrei-and-romainl). – Iulian Onofrei Nov 15 '16 at 11:02
  • 7
    Let aside what is wrong and what is correct. Is there a way to configure vim to show that final line? I mean to configure it to work "incorrectly" as most of Win/Mac editors do? – 4LegsDrivenCat Aug 21 '17 at 06:22
  • @4LegsDrivenCat, again, **there's no line to show**. – romainl Aug 21 '17 at 07:48
  • 5
    I said "Let aside what is wrong and what is correct". – 4LegsDrivenCat Aug 22 '17 at 11:48
  • And I said "there's no line to show". Vim can't be configured to show a line that doesn't exist. – romainl Aug 22 '17 at 12:16
  • Please reread the comment of neverhoodboy above. It explains a lot. More info on wikipedia. – 4LegsDrivenCat Aug 31 '17 at 22:22
  • I already read it two years ago and I have written [another longer answer](https://stackoverflow.com/a/16224292/546861) to a similar question two years before. There is no line to display and no way to force Vim to display a line that's not here, no matter if you are comfortable with that behavior or not. – romainl Sep 01 '17 at 05:38
  • 1
    `Ga` is "jump to last line and append after the cursor" so there's no reason whatsoever to use that command to "jump to last line and add a new line". You would use `Go` for that. – romainl Feb 28 '18 at 19:57
  • 1
    Not only is not "wrong" to treat a newline as a separator, there is no newline character. There's a linefeed character, which tells a line printer to move the paper forward, and a carriage return character. How those get repurposed in ASCII files is a matter of convention, and varies with operating system and software. (Which also means that whether the newline character implies the existence of a blank line after it is, again, down to what you're using.) Vim's default happens to be most convenient for writing version-controlled code. – johncip Aug 27 '20 at 17:54