在使用Visual Studio代码时,如何自定义制表符到空格的转换因子?

例如,现在在HTML中,每次按TAB键会产生两个空格,但在TypeScript中会产生4个空格。


当前回答

我不得不像之前的答案一样做了很多设置编辑,所以我不知道在做了很多修改后它能正常工作。

在我关闭并打开我的IDE之前没有任何工作,但我所做的最后三件事是禁用lonefy。html.format vscode-js-css-html-formatter。”enable": true,并重新启动Visual Studio。

{
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "workbench.colorTheme": "Default Light+",
    "[html]": {
        "editor.defaultFormatter": "vscode.html-language-features",
        "editor.tabSize": 2,
        "editor.detectIndentation": false,
        "editor.insertSpaces": true
    },
    "typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": true,
    "editor.tabSize": 2,
    "typescript.format.insertSpaceAfterConstructor": true,
    "files.autoSave": "afterDelay",
    "html.format.indentHandlebars": true,
    "html.format.indentInnerHtml": true,
    "html.format.enable": true,
    "editor.detectIndentation": false,
    "editor.insertSpaces": true,
}

其他回答

我运行的是版本1.21,但我认为这也适用于其他版本。

看看屏幕的右下角。您应该看到显示空格或制表符大小的内容。

我的是空格→

点击空格(或Tab-Size) 选择“使用空格缩进”或“使用制表符缩进” 选择所需的空格或制表符数量。

这只适用于每个文档,而不是整个项目。如果你想在项目范围内应用它,你还需要在你的用户设置中添加"editor.detectIndentation": false。

我们亲爱的社区成员已经提供了很多很好的答案。我实际上想添加c#代码tabSize,并找到了这个线程。我找到了很多解决方案,官方VS Code文档很棒。我只是想分享我的c#设置:

"[csharp]": {
    "editor.insertSpaces": true,
    "editor.tabSize": 4
},

只需复制并粘贴上述代码到您的设置。Json文件并保存。谢谢

评论如下:

有没有办法改变每一种语言的tabSize ?例如,当在同一个工作区中编辑多个不同语言的文件时(如Ruby, JavaScript, CSS等)- Ruby将是2个空格,但CSS将是4个…通常

这就是为什么,VSCode 1.63(2021年11月),你有:

多种语言特定的编辑器设置 现在,您可以一次为多种语言配置特定于语言的编辑器设置。 下面的例子展示了如何同时自定义javascript和typescript语言的设置: “[javascript](打印稿)":{ “编辑器。maxTokenizationLineLength”:2500 }

在你的情况下:

"[ruby][html]": {
    "editor.insertSpaces": true,
    "editor.tabSize": 2
},
"[csharp][typescript]": {
    "editor.insertSpaces": true,
    "editor.tabSize": 4
},

你也可以在终端中改变制表符的大小,使用VSCode 1.75(2023年1月)。

我们可以用EditorConfig和它的EditorConfig for VS Code扩展来控制文件类型的选项卡大小。然后,我们可以对每种文件类型使用Alt + Shift + F。

安装

用CTRL + P打开VS Code命令面板并粘贴如下:

ext install EditorConfig

示例配置

editorconfig。

[*]
indent_style = space

[*.{js,ts,json}]
indent_size = 2

[*.java]
indent_size = 4

[*.go]
indent_style = tab

settings.json

EditorConfig覆盖任何设置。Json为编辑器配置。不需要更改editor.detectIndentation。

如果这是针对Angular 2的,并且CLI正在生成你想要不同格式的文件,你可以编辑这些文件来改变生成的内容:

npm_modules/@angular/cli/blueprints/component/files/__path__/*

不推荐大量的npm更新,因为它会删除你的工作,但它为我节省了很多时间。