You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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:

Fixing VS Code Auto-Format to Add Space Before Braces

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 Spacing and make sure it’s checked
  • Alternatively, add this to your project’s .prettierrc file 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.js or .eslintrc.json file:
    {
      "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 Save in 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 statement and Method declaration, set the option to Same line with space
  • For a more visual approach, open the command panel (Ctrl+Shift+P/Cmd+Shift+P), search for Java: 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 Save in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:50:56