在使用Visual Studio代码时,如何自定义制表符到空格的转换因子?
例如,现在在HTML中,每次按TAB键会产生两个空格,但在TypeScript中会产生4个空格。
在使用Visual Studio代码时,如何自定义制表符到空格的转换因子?
例如,现在在HTML中,每次按TAB键会产生两个空格,但在TypeScript中会产生4个空格。
当前回答
你想要确保你的编辑配置不与你的用户或工作区设置配置冲突,因为我只是有一点烦恼,认为设置文件设置没有被应用时,它是我的编辑器配置撤消这些更改。
其他回答
默认情况下,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
User3550138正确。lonefy。vcode -js-css-html-formatter覆盖其他答案中提到的所有设置。但是,您不必禁用或卸载它,因为它可以配置。
可以通过打开扩展侧边栏并单击该扩展来找到完整的说明,它将在编辑器工作区中显示配置说明。至少在Visual Studio Code版本1.14.1中对我来说是这样的。
一个更简单的方法是使用Visual Studio Code提供的内置通配符过滤器@lang:。
步骤
点击设置图标(左下) 选择设置 在搜索设置输入框中输入@lang:javascript tabsize
这将过滤所有的设置,只留下javascript的tabSize选项。输入您想要的tab因子,它将应用于该语言。
请注意 要将这些设置应用到用户配置文件,请确保选择了user选项卡。如果选择Workspace选项卡,VSCode将在你的项目文件夹中创建一个带有设置的. VSCode文件夹。json文件;这意味着您所做的任何更改都只应用于当前工作空间(项目文件夹) 你可以将它用于任何其他语言,例如@lang:php tabsize
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文件中的根项目级别添加/更新
菜单文件→首选项→设置
添加到用户设置:
"editor.tabSize": 2,
"editor.detectIndentation": false
然后右键单击你的文档,如果你已经打开了一个,然后单击格式化文档,让你现有的文档遵循这些新的设置。