Reputation: 8954
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?
Upvotes: 344
Views: 208115
Reputation: 635
If you wish to apply this to just your current project, you can achieve that by creating your settings.json
file in a .vscode
directory in your project's root directory - PROJECT_ROOT/.vscode/settings.json
Paste in the following into the settings.json
file
{
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true
}
Henceforth, on save of a file in your project;
files.insertFinalNewline
will add a trailing new line to the end of the filefiles.trimFinalNewlines
will trim off any extra newlines at the end of the file; making sure there's just one newline (assuming files.insertFinalNewline
is set to true
)Upvotes: 5
Reputation: 6373
VS Code 1.76.2
.
CMD
+ Shift
+ P
user settings JSON
settings.json
add"files.insertFinalNewline": true,
"files.trimFinalNewlines": true
OR
CMD
+ ,
newline
Insert Final Newline
and Trim Final Newlines
Upvotes: 21
Reputation: 21
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
Upvotes: 0
Reputation: 1
I was experiencing the same issue and solved it by uninstalling a save while typing extension that I had installed.
Upvotes: -1
Reputation: 4178
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)
Upvotes: 3
Reputation: 70479
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".
Upvotes: 147
Reputation: 8954
There are two easy methods to make Visual Studio Code insert a new line at the end of files:
Open Visual Studio Code and go to File (Code if using a Mac) -> Preferences -> Settings; you should now be viewing a settings page
Enter 'insert final newline' in to the search bar
Select the checkbox under the heading 'Files: Insert Final Newline' in the 'Workspace Settings' and/or 'User Settings' tab(s) as required
Open Visual Studio Code and go to File (Code if using a Mac) -> Preferences -> Settings; you should now be viewing a settings page
Open the JSON settings page by clicking the {} icon at the top right of the page
Enter 'files.insertFinalNewline' in to the search bar of the JSON settings page
Either
files.insertFinalNewline
JSON key and select True
or
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
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.
Upvotes: 512