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

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


当前回答

如果这篇文章中接受的答案不奏效,试试这个:

我在编辑器中安装了用于Visual Studio Code的EditorConfig,它一直覆盖我的用户设置,这些设置被设置为使用空格缩进文件。每次我在编辑器选项卡之间切换时,我的文件都会自动用选项卡缩进,即使我已经将缩进转换为空格!!

在我卸载了这个扩展后,缩进不再改变切换编辑器选项卡之间,我可以更舒适地工作,而不是必须手动转换选项卡到空格每次我切换文件-这是痛苦的。

其他回答

评论如下:

有没有办法改变每一种语言的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月)。

默认情况下,Visual Studio Code将根据您打开的文件尝试猜测缩进选项。

你可以通过"editor.detectIndentation": false关闭缩进猜测。

您可以通过以下三个设置轻松自定义:Windows菜单中的文件→首选项→用户设置,Mac菜单中的代码→首选项→设置或⌘,:

// The number of spaces a tab is equal to. This setting is overridden
// based on the file contents when `editor.detectIndentation` is true.
"editor.tabSize": 4,

// Insert spaces when pressing Tab. This setting is overriden
// based on the file contents when `editor.detectIndentation` is true.
"editor.insertSpaces": true,

// When opening a file, `editor.tabSize` and `editor.insertSpaces`
// will be detected based on the file contents. Set to false to keep
// the values you've explicitly set, above.
"editor.detectIndentation": false

Note

如果你正在谈论tabSize的更漂亮,请参阅答案的第2部分

第一部分:VS代码方式

如果你喜欢开发人员的方式,Visual Studio Code允许你为tabSize指定不同的文件类型。下面是我的设置示例。json默认有四个空格,JavaScript/ json有两个空格:

{
  // I want my default to be 4, but JavaScript/JSON to be 2
  "editor.tabSize": 4,
  "[javascript]": {
    "editor.tabSize": 2
  },
  "[json]": {
    "editor.tabSize": 2
  },

  // This one forces the tab to be **space**
  "editor.insertSpaces": true
}

PS:好吧,如果你不知道如何打开这个文件(特别是在Visual Studio Code的新版本中),你可以:

左下档→ 设置→右上方打开设置


第二节:如果使用更漂亮

如果你使用的是prettier,情况可能会有所不同,prettier有两个级别的设置:

用户级别,你可以点击扩展和点击设置找到关键字tabWidth 项目级别,您可以从. pretierrc文件中的根项目级别添加/更新

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

在我关闭并打开我的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,
}

那是孤独。要怪vcode -js-css-html-formatter。禁用它,并安装HookyQR.beautify。

现在保存你的标签不会被转换。