282

When saving a file using Visual Studio Code, a newline is not automatically added to the end of the file, causing all sorts of potential issues.

How can I append a newline automatically in Visual Studio Code?

John Smith
  • 7,243
  • 6
  • 49
  • 61
LJH
  • 7,444
  • 3
  • 10
  • 19
  • 1
    It does indeed cause all sorts of potential issues, but it is also a beginner mistake of compiler writers, parser writers, and other system writers not to handle it properly. – Peter Mortensen May 17 '21 at 18:26
  • 5
    @Peter Mortensen having a line break as the last character in a file does not just prevent issues, it's in the POSIX standard, so tools are allowed to assume it. – altermetax Aug 24 '22 at 13:31

6 Answers6

426

There are two easy methods to make Visual Studio Code insert a new line at the end of files:

Method I

  1. Open Visual Studio Code and go to File (Code if using a Mac) -> Preferences -> Settings; you should now be viewing a settings page

  2. Enter 'insert final newline' in to the search bar

  3. Select the checkbox under the heading 'Files: Insert Final Newline' in the 'Workspace Settings' and/or 'User Settings' tab(s) as required

Settings Page with 'Files: Insert Final Newline' checkbox selected

Method II

  1. Open Visual Studio Code and go to File (Code if using a Mac) -> Preferences -> Settings; you should now be viewing a settings page

  2. Open the JSON settings page by clicking the {} icon at the top right of the page

  3. Enter 'files.insertFinalNewline' in to the search bar of the JSON settings page

  4. Either

    • Click on the white 'edit pen' on the left hand side of the line containing the files.insertFinalNewline JSON key and select True

    or

    • Copy the line containing the files.insertFinalNewline JSON key, paste it into the right hand side JSON file under the 'User Settings' and/or 'Workspace Settings' tab(s) as required, and set its value to true

User Settings JSON with <code>files.insertFinalNewline</code> set to <code>true</code>

Final Result

In either your User Settings or Workspace Settings JSON file, you should have a line reading "files.insertFinalNewline": true, within the provided curly braces ({ }). Additionally, in the Settings page, the checkbox under the heading 'Files: Insert Final Newline' will be selected.

Visual Studio Code will now add an empty line to the end of files when being saved, if there isn't already one.

LJH
  • 7,444
  • 3
  • 10
  • 19
  • 3
    Does this ever result in extra newlines if the file already has newlines at the end? – Aaron Franke Oct 29 '19 at 03:13
  • 3
    @AaronFranke I have never seen that behaviour. For me, it only adds a newline if there isn't already one. If you are having this issue, perhaps check your final lines don't contain some formatting that you aren't noticing. – LJH Oct 30 '19 at 10:07
  • 9
    You can also define per language type: `"[javascript]": {"files.insertFinalNewline": true}` – Get Off My Lawn Jun 29 '20 at 20:30
  • 1
    @AaronFranke No, but if you already have extra newlines, this setting doesn't cap the extras. This is sad. – Lawrence Nov 13 '20 at 14:36
  • 3
    @Lawrence but there is also a setting for that `"files.trimFinalNewlines": true` – Davos May 10 '21 at 15:03
  • My VSCode is suddenly adding a blank line at the end of file when I save. I have disabled all options to add a new line. I even uninstalled the `Prettier` extension. But it still keeps adding a new blank line at the end of file when I press `ctrl + s`. Does anyone know why? – CatarinaRuna Sep 20 '21 at 07:16
  • @0x0147k3r I have not had to restart VS Code after applying the files.insertFinalNewline setting on any of the machines I have used. – LJH Jun 07 '22 at 16:21
  • @LJH for me I needed to restart, I removed the comment as I see it was of no use to anyone else "thumbs up" – 0xe1λ7r Jun 08 '22 at 12:47
  • Is there a way to achieve similar result in Visual Studio (not Code)? – Cheburek Nov 16 '22 at 20:09
138

I have placed a screen capture below showing how to make Visual Studio Code insert a newline at the end of files. This will also serve as a useful place to link to in code reviews when saying "You need to do this and resubmit".

screen capture of settings change

LJH
  • 7,444
  • 3
  • 10
  • 19
Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
  • 8
    Awesome, I also recommend ticking the checkbox `Trim Final Newlines` in case there are multiple newlines to reduce it to just 1. I also like `Trim Trailing Whitespace` although some markdown requires two trailing spaces, I avoid that particular markdown syntax because I really want to trim it in code files. – Davos May 10 '21 at 15:11
7

VS Code 1.76.2.

  1. CMD + Shift + P
  2. Type user settings JSON

Setting search

  1. In settings.json add
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true

OR


  1. CMD + ,
  2. Type in the search settings newline

Setting search

  1. Check Insert Final Newline and Trim Final Newlines

Files settings

Wojciech Bednarski
  • 6,033
  • 9
  • 49
  • 73
3

While editing PHP files, something was deleting the newline at the end of the file. In my case it turned out to be the HTML format (html.format.endWithNewline), I believe because of the Emmet extension. (see attached images)

emmet settings html format "End with newline" setting

Telmo Dias
  • 3,938
  • 2
  • 36
  • 48
0

I was experiencing the same issue and solved it by uninstalling a save while typing extension that I had installed.

Victor
  • 1
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 21 '23 at 11:43
0

every type of files has it's own settings so if you go to the settings you will find for example for blade: Blade: New Line then a check box to insert or not an empty line at the end of file

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Alan Bosco Feb 23 '23 at 10:38