如何在Visual Studio Code中设置大括号格式化:大括号前保留空格
Hey there! I totally get how tedious manual formatting can be—let’s fix this so VS Code does the heavy lifting for you. Here’s how to get that desired if(a == b) { // My Code } and public void method_name(parameter list) { //My Code } style automatically:
First, confirm your default formatter
VS Code uses different formatting tools depending on your language, so let’s check which one’s in charge:
- Open VS Code settings (hit
Ctrl+,on Windows/Linux,Cmd+,on Mac) - Search for
default formatter—note down the tool listed (e.g., Prettier, ESLint, Language Support for Java, or the Visual Studio Code Format plugin you installed)
Configure based on your formatter
1. If you’re using Prettier (common for JS/TS/CSS)
Prettier actually defaults to adding spaces before braces, but if your settings got messed up:
- In settings, search for
Prettier: Bracket Spacingand make sure it’s checked - Alternatively, add this to your project’s
.prettierrcfile to lock it in:{ "bracketSpacing": true }
2. If you’re using ESLint (for front-end projects)
ESLint needs explicit rules to enforce this spacing:
- Add these rules to your
.eslintrc.jsor.eslintrc.jsonfile:{ "rules": { "space-before-blocks": "error", // Forces a space before all braces "keyword-spacing": ["error", { "before": true }] // Ensures spaces after keywords like `if`/`function` (optional but cleans up code further) } } - Don’t forget to enable
ESLint: Auto Fix On Savein settings so ESLint fixes formatting when you save.
3. If you’re working with Java (using Language Support for Java plugin)
Java’s formatting is controlled via the plugin’s settings:
- Open settings and search for
Java > Formatting: Braces - For
Control statementandMethod declaration, set the option toSame line with space - For a more visual approach, open the command panel (
Ctrl+Shift+P/Cmd+Shift+P), search forJava: Open Java Formatter Settings with Preview, tweak the brace spacing options, and save the config.
4. For the Visual Studio Code Format plugin you installed
This plugin has language-specific settings to adjust brace spacing:
- Open settings, search for
vscode-format - Find the section for your language (e.g., JavaScript, Java) and add a setting like:
"vscode-format.javascript": { "spaceBeforeBlock": true } - Look for any checkbox labeled "Add space before braces" if available—it’ll do the same thing.
Make sure auto-format is actually working
No matter which tool you use, double-check these two settings:
- Enable
Editor: Format On Savein settings so VS Code formats your code automatically when you save - If you have multiple formatting plugins installed, ensure your preferred tool is set as the
default formatter(we checked this earlier!)
Test it out
Write a snippet like this:
if(a == b){ // My Code } public void method_name(parameter list){ //My Code }
Save the file—VS Code should auto-convert it to your desired style:
if(a == b) { // My Code } public void method_name(parameter list) { //My Code }
If it’s still not working, try temporarily disabling other formatting plugins to rule out conflicts—sometimes multiple tools fighting over formatting can cause issues.
内容的提问来源于stack exchange,提问作者SHAH MD IMRAN HOSSAIN

