Vim show newline at the end of file

14,591

'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.

Share:
14,591
Keith Smiley
Author by

Keith Smiley

iOS & Mac developer.

Updated on June 04, 2022

Comments

  • Keith Smiley
    Keith Smiley 12 months

    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.

  • Keith Smiley
    Keith Smiley about 10 years
    One thing to note is that this behavior is by every native OS X text editor, not just TextMate
  • romainl
    romainl about 10 years
    Could you post your file? I don't see that pesky line here on the two latest version of textmate I just downloaded.
  • Keith Smiley
    Keith Smiley about 10 years
    go to your last line, press enter, save it, open in vim, that line you just created won't be there.
  • neverhoodboy
    neverhoodboy about 8 years
    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
    neverhoodboy about 8 years
    Add the [noeol] sign will only be there right after you open the file. It will disappear once you perform any non-trivial operation.
  • Iulian Onofrei
    Iulian Onofrei over 6 years
    TextMate's behavior isn't wrong at all! Every editor in the world shows that empty line which vim doesn't display!
  • Iulian Onofrei
    Iulian Onofrei over 6 years
    @KeithSmiley, How did you solve the problem? Why did you accepted this as a solution?
  • romainl
    romainl over 6 years
    @IulianOnofrei, there's no line to display.
  • Iulian Onofrei
    Iulian Onofrei over 6 years
    @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.
  • romainl
    romainl over 6 years
    @IulianOnofrei, no, line 2 is not gone.
  • Iulian Onofrei
    Iulian Onofrei over 6 years
    @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
    Iulian Onofrei over 6 years
  • 4LegsDrivenCat
    4LegsDrivenCat almost 6 years
    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?
  • romainl
    romainl almost 6 years
    @4LegsDrivenCat, again, there's no line to show.
  • 4LegsDrivenCat
    4LegsDrivenCat almost 6 years
    I said "Let aside what is wrong and what is correct".
  • romainl
    romainl almost 6 years
    And I said "there's no line to show". Vim can't be configured to show a line that doesn't exist.
  • 4LegsDrivenCat
    4LegsDrivenCat over 5 years
    Please reread the comment of neverhoodboy above. It explains a lot. More info on wikipedia.
  • romainl
    romainl over 5 years
    I already read it two years ago and I have written another longer answer 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
    romainl about 5 years
    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.
  • johncip
    johncip over 2 years
    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.