0

As far as I'm concerned, there are the following options when it comes to the "final newline" in a file (considering only Unix-style line endings and disregarding Classic MacOS and DOS, for now):

  1. No final newline character:
    first line\n
    last line
    
  2. Newline character at the end of the last line:
    first line\n
    last line\n
    
  3. A whole final new line:
    first line\n
    last line, except not really\n
    \n
    

Which ones of these exactly are achieved by setting insert_final_newline to true and false, respectively, in .editorconfig?

P Varga
  • 19,174
  • 12
  • 70
  • 108

1 Answers1

1

The official definition of that property is:

insert_final_newline: set to "true" to ensure file ends with a newline when saving and "false" to ensure it doesn't.

That means:

  1. insert_final_newline = true: This setting ensures that there is a newline character at the end of the last line in the file. That is your option 2:

    first line\n
    last line\n
    
  2. insert_final_newline = false: This setting allows files to end without a newline character at the end of the last line. That is your option 1:

    first line\n
    last line
    

The .editorconfig setting does not provide an option for adding an entirely new line after the last line with content, as in your option 3. That behavior would need to be manually added or configured through a different method, such as using a custom script or plugin for your editor.

See also "Why should text files end with a newline?", which illustrates your options 1 and 2, but not 3.

It refers to the POSIX convention (enter link description here) which defines a line as "a sequence of zero or more non-<newline> characters plus a terminating <newline> character"

Therefore:

No (sane) tool would count the last EOL (CR, LF, etc) of a file as an additional, empty line.
And all POSIX tools will not count the last characters of a file as a line if there is no ending EOL.

Regardless of the EOL character name being "line feed" or "carriage return" (there's no character named "newline"), for all practical puposes sensible tools treat it as a line terminator, not as a line separator.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250