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

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

更新

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


当前回答

第一步:打开设置。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
}

其他回答

我想把现有HTML文件的缩进从4个空格改为2个空格。

我点击了状态栏中的“空格:4”按钮,并在下一个对话框中将它们更改为2。

我使用'vim'扩展。我不知道如何重新缩进没有vim

为了重新缩进我的当前文件,我使用了这个:

gg

=

G

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

你可以打开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"
}

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

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

第一步:打开设置。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
}

使用实例根据编程语言修改缩进。

打开命令面板(CtrlShiftP | macOS:⌘P)。 键入并选择:首选项:配置语言特定设置…(命令id: workbench.action.configureLanguageBasedSettings)。 选择一种编程语言(例如TypeScript)。

如果打开Settings菜单(自1.66.0起): 4. 按→将光标放在语言筛选器的右边(例如@lang:typescript)。 5. 输入“Tab Size”,并在文本框中输入您的首选值。 如果设置。Json文件被打开: 4. 添加以下代码: “(打印稿)":{ “编辑器。tabSize”:2 }

参见:VS Code Docs