Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code — Insert New Line at the End of Files

People also ask

How do you skip to the end of a line in VS Code?

Highlight the row and press ENTER. Press Shift + Space.

How do I insert a line break in Visual Studio?

1 Answer. Show activity on this post. Enable "Use regular expressions" mode (the icon with the small square and the star), and enter \r\n as the code for line breaks. If you want to format your code, you might also need \t .

Why is newline required at end of file?

The way Unix regards its general behavior at the end of files is as follows: \n characters don't start lines; instead, they end them. So, \n is a line terminator, not a line separator. The first line (like all lines) needs no \n to start it. The last line (like all lines) needs an \n to end it.


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.


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


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