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

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


当前回答

你想要确保你的编辑配置不与你的用户或工作区设置配置冲突,因为我只是有一点烦恼,认为设置文件设置没有被应用时,它是我的编辑器配置撤消这些更改。

其他回答

如果您在Visual Studio Code中使用更漂亮的扩展,请尝试将此添加到设置中。json文件:

"editor.insertSpaces": false,
"editor.tabSize": 4,
"editor.detectIndentation": false,

"prettier.tabWidth": 4,
"prettier.useTabs": true  // This made it finally work for me

默认情况下,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

我们可以用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。

当使用TypeScript时,默认的选项卡宽度总是2,不管它在工具栏中说什么。你必须设置“更漂亮”。tabWidth"在你的用户设置中更改它。

Ctrl + P,输入→用户设置,添加:

"prettier.tabWidth": 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,
}