如何设置标签空间样式?

一个简单的问题:使用VSCode时,是否可以自定义Tab-to-Space转换因子? 例如,现在在HTML中,每按一次TAB就会产生2个空格,但在打字稿中它会产生4个空格。

默认情况下,VSCode将根据您打开的文件尝试猜测您的缩进选项。

您可以通过"editor.detectIndentation": falseclosures缩进猜测"editor.detectIndentation": false

您可以通过File > Preferences > User Settings这3个设置轻松地自定义:

 // The number of spaces a tab is equal to. This setting is overriden // 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. "editor.detectIndentation": true 

我正在运行版本1.21,但我认为这也可能适用于早期版本。

看看屏幕的右下angular。 你应该看到一些说SpacesTab-Size

我的显示空间, – > 在这里输入图像说明

  1. 点击Spaces (或Tab-Size
  2. selectIndent Using SpacesIndent using Tabs
  3. select你喜欢的空间或标签的数量。

默认情况下,VSCode自动检测当前打开文件的缩进。 如果您想closures此function并进行缩进(例如2个空格),则可以在用户设置或工作区设置中执行以下操作。

 { "editor.tabSize": 2, "editor.detectIndentation": false } 

我们可以通过使用EditorConfig 的文件types和VS代码扩展来控制标签大小 。 然后,我们可以针对每个文件types制作Alt + Shift + F。

安装

ext install EditorConfig

示例configuration

.editorconfig

 [*] indent_style = space [*.{cs,js}] indent_size = 4 [*.json] indent_size = 2 

settings.json

EditorConfig覆盖任何settings.json为编辑器configuration的内容。 没有必要改变editor.detectIndentation

那么,新版本的vscode允许你为tabSize指定不同的文件types,这里是我的settings.json的例子,默认4个空格和js / json 2个空格:

 { "editor.fontSize": 20, "editor.tabSize": 4, "[javascript]": { "editor.tabSize": 2 }, "[json]": { "editor.tabSize": 2 } } 

PS:好吧,如果你不知道如何打开这个文件,你可以:点击左下angular的gear-icon/Settings

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

这是lonefy.vscode-js-css-html-formatter怪,禁用它,然后安装HookyQR.beautify

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

User3550138是正确的lonefy.vscode-js-css-html-formatter覆盖了其他答案中提到的所有设置,但是您不必禁用或卸载它,因为它可以configuration。 完整的说明可以通过打开扩展补充工具栏并单击该扩展名find,它将在编辑器工作区中显示configuration说明。 至less它在VS Code版本1.14.1上对我有用。

如果是Angular 2,并且CLI生成的文件格式不同,则可以编辑这些文件以更改生成的内容:

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

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