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

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


当前回答

一个更简单的方法是使用Visual Studio Code提供的内置通配符过滤器@lang:。

步骤

点击设置图标(左下) 选择设置 在搜索设置输入框中输入@lang:javascript tabsize

这将过滤所有的设置,只留下javascript的tabSize选项。输入您想要的tab因子,它将应用于该语言。

请注意 要将这些设置应用到用户配置文件,请确保选择了user选项卡。如果选择Workspace选项卡,VSCode将在你的项目文件夹中创建一个带有设置的. VSCode文件夹。json文件;这意味着您所做的任何更改都只应用于当前工作空间(项目文件夹) 你可以将它用于任何其他语言,例如@lang:php tabsize

其他回答

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

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

我的是空格→

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

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

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

默认情况下,Visual Studio Code自动检测当前打开文件的缩进。如果您想关闭此功能并使所有缩进,例如,两个空格,您可以在用户设置或工作区设置中执行以下操作。

{
    "editor.tabSize": 2,

    "editor.detectIndentation": false
}

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