20

Why does VS add a blank line at the end of each new file I create? I use VS to create .NET projects (not C++ or something).

Is there any special reason? Historical compatibility with compilers and parsers?

Can I disable this??

Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
  • 2
    Some compilers / tools in the past have had difficulty with files where the last line doesn't end in a carriage return. But you're unlikely to be able to find the *actual* answer... – forsvarir Jul 14 '11 at 09:23
  • 2
    Please always add a newline at the end of file - please – Mr_and_Mrs_D Mar 28 '15 at 13:16
  • On Linux, if you `cat` a file from the terminal, and it doesn't end in a line break, your next terminal prompt will appear at the end of the last line of the file, rather than on it's own new line. Not sure about the corresponding Windows `type` command. – Daniel Stevens Apr 20 '19 at 11:50

2 Answers2

9

It is recommended to have a blank line at the end of each file for navigability purposes. Think what happens if someone opens your code with vim for example and uses a keymap to jump from empty line to empty line. Or, if he decides to add another portion of code at the end of file he can quickly do a GO and enter it instead of having to scroll until the end of file.

Also, source control tools will report more changes if you don't have a trailing blank line when you try to insert code at the end of file.

Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
  • 1
    So what happens if someone opens my code with vim? BTW, what's the m in vim? – Andrei Rînea Jul 14 '11 at 09:25
  • 1
    I know, I was trying to hint that it's called improved but it's not improved enough ;) – Andrei Rînea Jul 14 '11 at 09:36
  • 12
    Vim deals just fine with files without blank lines at the end, it doesn't break `G` navigation at all. Jumping from blank line to blank line (I'm assuming you mean using `{`/`}`) also deals fine with this. The source control reason also seems kind of silly, especially considering that in C# for example you never insert code right at the end, as it's always in a namespace/class block anyway. – Strigoides Feb 03 '14 at 22:06
  • 1
    A text editor shows you a new line. But technically there isn't one. The newline char is part of the last line not the new line. So it's good to have all lines end with a `CR LF` / `LF` – Info-Screen Dec 01 '16 at 19:57
  • 4
    The real reason is probably in this answer: https://stackoverflow.com/a/729795/1178235 It's not about recommendation, but about POSIX compliance (which is not cosmetic, but guarantees you interoperability). – Alessio Gaeta Dec 19 '18 at 15:04
  • @Strigoides well, now as of C#9 you can write top-level programs in where you totally are going to add code to the end of the file :) – EluciusFTW Apr 27 '21 at 13:21
  • Also, source control tools will report more changes if you don't have a trailing blank line when you try to insert code at the end of file. => Can you make an example? – Lukas Salich Nov 19 '21 at 15:31
0

For those who want to get rid of the new line at the end of a file:

I had a similar issue and the solution provided by 9paradox in this post solved my problem.

When I was saving my code (Python, JSON, JS, etc...) I got always a new line inserted at the end of the file.

Solution: if, e.g. on Mac, you are editing any file in your project, then go to the root directory of your project, let's say /Users/<username>/git/<project> then search for .editorconfig in that folder, then open that file with any text editor like vi or TextEdit and make insert_final_newline = false. VS Code takes over the settings changes immediately. No restart needed.

Tony
  • 7,767
  • 2
  • 22
  • 51