对于每个typescript文件,visual studio代码使用8个空格的自动缩进。这对我来说有点太贵了,但我找不到换的地方。

也许它可以作为一个设置,但在不同的名称下,因为我找不到任何与缩进有关的东西。

更新

我目前正在使用更漂亮的代码格式化器,它解决了所有的格式问题,自动格式化保存(如果没有语法错误)


当前回答

我喜欢这些缩进设置,你可以根据需要修改它们。

你可以打开VScode设置。输入CTRL+SHIFT+P并粘贴到json设置下面

setting.json

"[javascript]": {
    "editor.defaultFormatter": "vscode.typescript-language-features",
    "editor.formatOnSave": true,
    "editor.tabSize": 4,
    "editor.insertSpaces": false,
    "editor.detectIndentation": false,
    "editor.wrappingIndent": "deepIndent", 
    "editor.autoIndent": "full"
},
"[typescript]": {
    "editor.defaultFormatter": "vscode.typescript-language-features",
    "editor.formatOnSave": true,
    "editor.tabSize": 4,
    "editor.insertSpaces": false,
    "editor.detectIndentation": false,
    "editor.wrappingIndent": "deepIndent", 
    "editor.autoIndent": "full"
}

其他回答

如何在VS Code中把所有文件中的4个空格缩进到2个空格

打开文件搜索 打开正则表达式 输入:({2})(?: {2}) (\ b |(?!=[,'";\.:\*\\\/\{\}\[\]\(\)])) 在搜索领域 在replace字段中输入:$1

如何在VS Code中把所有文件中的2个空格缩进到4个空格

打开文件搜索 打开正则表达式 输入:({2})(\ b |(?!=[,'";\.:\\*\\\/{\}\[\]\(\)])) 在搜索领域 在replace字段中输入:$1$1

注意:您必须首先打开PERL Regex。这是如何做到的:

打开设置,转到JSON文件 将以下内容添加到JSON文件“search”中。usePCRE2”:真正的

希望有人能看到。

第一步:打开设置。vscode中的Json

步骤2:为编程语言添加如下代码行(示例如下)

用于typescript和javascript

"editor.detectIndentation": false,
"[typescript]": {
        "editor.defaultFormatter": "vscode.typescript-language-features",
        "editor.tabSize": 2,
        "editor.insertSpaces": true,
        "editor.detectIndentation":false
 },
"[javascript]": {
    "editor.tabSize": 2,
    "editor.insertSpaces": true,
    "editor.detectIndentation":false
}

首先,检查你是否安装了“EditorConfig for VS Code”。它覆盖了我的编辑器设置。我花了一整天修正这个问题。

在项目中找到.editorconfig文件,并在那里更改它将工作。

如果你正在使用格式化器,检查tabWidth,这是我的情况下的问题。它表示制表符中使用的空格数。

除了elliott - j的答案之外,您可能还希望将editor.detectIndentation设置为false。

VSCode会覆盖你的编辑器。tabSize和编辑器。如果它检测到文件具有不同的制表符或空格缩进模式,则为每个文件添加insertSpaces设置。如果你将现有文件添加到项目中,或者使用Angular Cli等代码生成器添加文件,就会遇到这个问题。上面的设置阻止VSCode这样做。