New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] Invisible trailing newline should be the default on OSX and Linux #35181

Closed
cu opened this Issue Sep 26, 2017 · 4 comments

Comments

Projects
None yet
4 participants
@cu
Copy link

cu commented Sep 26, 2017

  • VSCode Version: 1.16.1
  • OS Version: Ubuntu 17.10

Reproduces without extensions: Yes

Description

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:

$ hexdump -C foo.txt 
00000000  6c 69 6e 65 20 6f 66 20  74 65 78 74 0a           |line of text.|

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.

Expected behavior

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.

Workarounds

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.

@alexandrudima

This comment has been minimized.

Copy link
Member

alexandrudima commented Sep 28, 2017

I agree, we could change the default of files.insertFinalNewline to true on OSX and Linux.

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.

@alexandrudima alexandrudima added this to the On Deck milestone Sep 28, 2017

@cu

This comment has been minimized.

Copy link
Author

cu commented Nov 22, 2017

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.

@BWPanda

This comment has been minimized.

Copy link

BWPanda commented Jan 29, 2018

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.
-- https://api.backdropcms.org/php-standards#indenting

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.

I hope that makes sense...

@alexandrudima alexandrudima modified the milestones: On Deck, Backlog Apr 20, 2018

@alexandrudima alexandrudima removed their assignment Apr 20, 2018

@alexandrudima

This comment has been minimized.

Copy link
Member

alexandrudima commented Sep 20, 2018

Let's track in #1027

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 4, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.