vscode does not automatically put a newline on the last line of a file in its default configuration. This is much different (and therefore surprising) behavior when compared with virtually all text manipulation tools for Unix-like OSes over the last few decades.
In Unix, a text file is composed of a series of zero or more lines. Each line is terminated by a newline (ASCII 0a) character. It follows then that unless the file has no characters, a text file should always end in a newline. For a simple example, we can use the shell to create a new file containing a string:
$ echo 'line of text' > foo.txt
Now if we look at the raw byes of the file, we see that a newline (0a) was automatically added to the file by the echo command:
This is desireable behavior because if you want to use standard Unix tools to work on the file, they will generally expect all lines to end in a newline whether it's printing the file to stdout, combining files, or performing complicated search and replace operations via regular expressions. We can see this in action by printing the file to stdout with cat:
$ cat foo.txt
line of text
Note that the shell prompt is one line after the file contents. The shell didn't do this, it was the result of the newline at the end of the file.
All other text editors operating in a Unix-like environment append a newline to the last (or only) line in the file. This includes nano, vim, emacs, etc. However, the trailing newline is not displayed and you cannot (normally) delete it. Contrast with vscode which does not append a trailing newline.
Steps to Reproduce
Open a new file (Ctrl+n)
Enter the exact string (without quotes) "line of text"
Save (Ctrl+s) the file as bar.txt
Now open the terminal and print the file to stdout:
$ cat bar.txt
line of text$
Notice how the shell prompt appears on the same line as the contents of the file, indicating the lack of a newline at the end of the file.
On Unix-based platforms (OSX and Linux), vscode should automatically append a newline to the end of every file as all other text editors for traditional Unix-like systems do. Further, it should not allow the user to see or remove the trailing newline by default, although a setting can be made for users who do want to see it.
I don't know how the Windows platform handles newlines so I defer to others on the platform-specific default behavior there.
There is a setting called files.insertFinalNewline but this is set to false by default on Unix-derived platforms. While it solves part of the problem, the final newline is displayed (as an empty line at the end of the file) and can be removed. Although saving the file again always brings it back.
The text was updated successfully, but these errors were encountered:
But, I would leave the implementation of this as is. i.e. allow the deletion of the last \n, only to have it come back on save.
Thanks for chiming in and having a look at this issue. But I believe it should not be displayed, for two reasons: 1) displaying it does not add any value (a text file always has a trailing newline, deleting it always brings it back anyway) and 2) it goes against the well-established behavior of traditional Unix text editors.
I'm open to hearing use cases where this wouldn't be desired, I myself haven't thought of any yet.
As an aside, the only reason I opened this issue was due to this point. I'm a little OCD about trailing whitespace and extraneous newlines. With files.insertFinalNewline set to true, I kept finding myself removing the trailing newline thinking "oh, that shouldn't be there" only to have it reappear every time I saved the file.
I develop websites with the Backdrop CMS (and previously the Drupal CMS). Both these CMS's have a coding standard that says:
All text files should end in a single newline (\n). This avoids the verbose "\ No newline at end of file" patch warning and makes patches easier to read since it's clearer what is being changed when lines are added to the end of a file.
I've therefore been developing code for the last few years in Linux by adding a new line at the end of each of my files.
I've just installed VS Code and opened one of my Backdrop files in it. It's displayed as having two new lines at the end of the file. I understand this to be what @cu explains above as follows:
The first blank line at the end of the file is the one I added to adhere to the coding standards of ending with a new line
The second one (which was never visible to me before in other editors) is because of how VS Code displays new lines in Linux (not hiding them like other editors do)
This means I can't do what I'd like to do by setting "files.insertFinalNewline": true and "files.trimFinalNewlines": true. I'd like to do that so that multiple new lines are removed, and so that at least one is present on save. However doing this means only one new line gets saved, which Linux then hides in other editors so therefore I'm not adhering to the coding standard.