VSCode Adds Space and New Line: Here's What You Need to Know
The Stack Exchange Network is a group of 183 Q&A communities, including Stack Overflow, the largest and most trusted online community for developers to learn, share, and build their careers. One common issue that developers face when using Visual Studio Code (VSCode) is the addition of space and new lines in their code.
Why Does VSCode Add Space and New Lines?
VSCode adds space and new lines to ensure that the code is properly formatted and indented. This makes the code easier to read and understand. However, this can be frustrating for developers who prefer to have complete control over the formatting of their code.
How to Prevent VSCode from Adding Space and New Lines
By default, VSCode has the "editor.formatOnSave" setting enabled. This means that every time you save a file, VSCode will automatically format it, adding space and new lines. To prevent this, follow these steps:
- Go to the VSCode settings (File > Preferences > Settings).
- Search for "editor.formatOnSave" in the search bar.
- Uncheck the box next to "editor.formatOnSave" to disable it.
- Go to the VSCode settings (File > Preferences > Settings).
- Search for "editor.defaultFormatter" in the search bar.
- Select the formatter that you want to use.
- You can also configure the formatter by creating a .prettierrc file in the root of your project and adding the desired settings. .
- Stack Exchange Network: https://stackexchange.com/sites
- Visual Studio Code: https://code.visualstudio.com/docs/getstarted/settings
- Prettier: https://prettier.io/docs/en/options.html
// Place your settings in this file to overwrite the default settings
{
"editor.formatOnSave": false
}
Alternatives to Disabling "editor.formatOnSave"
Instead of disabling "editor.formatOnSave" completely, you can customize how VSCode formats your code. Here's how:
// Place your settings in this file to overwrite the default settings
{
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
{
"tabWidth": 2,
"trailingComma": "es5",
"printWidth": 80
}
VSCode adds space and new lines to ensure that your code is properly formatted and indented. While this can be frustrating for some developers, you can customize how VSCode formats your code by changing the default formatter or configuring the formatter in a .prettierrc file. By taking the time to configure VSCode to your preferences, you can ensure that your code is formatted the way you want it to be.