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

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


当前回答

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

其他回答

在Visual Studio Code版本1.31.1或更高版本中(我认为):像sed Alex Dima一样,您可以通过这些设置轻松地自定义

Windows菜单中的文件→首选项→用户设置或使用短键Ctrl + Shift + P Mac在菜单代码→首选项→设置或⌘,

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

@ alexo -dima从2015年开始的解决方案将改变所有文件的制表符大小和空格,@Tricky从2016年开始的解决方案似乎只改变当前文件的设置。

截至2017年,我找到了另一种针对每种语言的解决方案。Visual Studio Code没有为Elixir使用合适的标签大小或空间设置,所以我发现我可以更改所有Elixir文件的设置。

我点击状态栏中的语言(在我的例子中是“Elixir”),选择“Configure 'Elixir' language based settings…”,并编辑Elixir特定的语言设置。我只是复制了“编辑器”。tabSize和编辑器。“insertSpaces”设置从左边的默认设置(我很高兴显示这些设置),然后在右边修改它们。

它工作得很好,现在所有Elixir语言文件都使用了适当的制表符大小和空间设置。

如果您在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

我试着换编辑器。tabSize改为4,但是. editorconfig将覆盖我所指定的任何设置,因此不需要更改用户设置中的任何配置。你只需要编辑.editorConfig文件:

set indent_size = 4