Visual Studio Code - Add a New Line to the End of Files

When saving files using Visual Studio code, I noticed that a new line is not automatically added to the end of the files, causing all kinds of potential problems.

How to insert a new line automatically in Visual Studio code?

+30
source share
2 answers

There are two easy ways to get Visual Studio code to insert a new line at the end of files:

Method I

  1. Open Visual Studio Code and go to " File" ("Code when using a Mac") β†’ "Settings" β†’ "Settings" ; You should now view the settings page.

  2. Type " insert last line" into the search bar

  3. Check the box under the heading β€œ Files: insert last line ” on the β€œWorkspace Settings ” and / or β€œ User Settings ” tabs , as required

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

Method II

  1. Open Visual Studio Code and go to " File" ("Code when using a Mac") β†’ "Settings" β†’ "Settings" ; You should now view the settings page.

  2. Open the JSON settings page by clicking the {} icon in the upper right corner of the page.

  3. Type ' files.insertFinalNewline ' into the search bar on the JSON settings page.

  4. Or

    • Click on the white β€œedit knob” on the left side of the line containing the JSON key files.insertFinalNewline and select True

    or

    • Copy the line containing files.insertFinalNewline JSON files.insertFinalNewline , paste it into the right JSON file on files.insertFinalNewline "User Settings" and / or "Workspace Settings" and set it to true

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

Final result

In the JSON file of user settings or workspace settings, there should be the line "files.insertFinalNewline": true , in the provided curly brackets ({}). In addition, a checkbox under the heading " Files: insert the last line " will be checked on the settings page.

Visual Studio Code now adds an empty line to the end of the file when saving, if it does not already exist.

+55
source

I put a screenshot below showing how to get Visual Studio code to insert a new line at the end of the files. It will also serve as a useful place for links in code reviews when you say, "You need to do this and resubmit."

screen capture of settings change

0
source

Source: https://habr.com/ru/post/1269118/


All Articles