在使用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

其他回答

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文件中的根项目级别添加/更新

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

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

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

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

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

set indent_size = 4

CTRL +逗号 使用制表符搜索缩进 去改变编辑器:标签大小

这是所有