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

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


当前回答

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

其他回答

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

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

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

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

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

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

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

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

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

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

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

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