本文翻译自:How to customize the tab-to-space conversion factor?
How to customize the tab-to-space conversion factor when using Visual Studio Code? 如何在使用Visual Studio代码时自定义制表符到空格的转换因子?
For instance, right now in HTML it appears to produce two spaces per press of TAB , but in TypeScript it produces 4. 例如,现在在HTML中,每按一次TAB就会生成两个空格,但在TypeScript中它会生成4个空格。
#1楼
参考:https://stackoom.com/question/21lBk/如何自定义制表符到空格的转换因子
#2楼
By default, Visual Studio Code will try to guess your indentation options depending on the file you open. 默认情况下,Visual Studio Code将尝试根据您打开的文件猜测缩进选项。
You can turn off indentation guessing via "editor.detectIndentation": false
. 您可以通过"editor.detectIndentation": false
关闭缩进猜测"editor.detectIndentation": false
。
You can customize this easily via these three settings for Windows in menu File → Preferences → User Settings and for Mac in menu Code → Preferences → Settings or ⌘,
: 你可以很容易地通过这三个设置在菜单File菜单代码 → 首选项 → 设置或自定义此为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
#3楼
I'm running version 1.21 but I think this may apply to earlier versions as well. 我正在运行1.21版,但我认为这也适用于早期版本。
Take a look at the bottom right-hand side of the screen. 看一下屏幕的右下角。 You should see something that says Spaces
or Tab-Size
. 您应该看到一些名为Spaces
或Tab-Size
。
Mine shows spaces, --> 我显示空间, - >
- Click on the
Spaces
(orTab-Size
) 单击Spaces
(或Tab-Size
) - Choose
Indent Using Spaces
orIndent using Tabs
选择Indent Using Spaces
或Indent using Tabs
- Select the amount of spaces or tabs you like. 选择您喜欢的空格或标签数量。
This only works per document, not project-wide. 这仅适用于每个文档,而不适用于整个项目。 If you want to apply it project-wide, you need to also add "editor.detectIndentation": false
to your user settings. 如果要在项目范围内应用它,还需要在用户设置中添加"editor.detectIndentation": false
。
#4楼
By default, Visual Studio Code auto-detects the indentation of the current open file. 默认情况下,Visual Studio代码会自动检测当前打开文件的缩进。 If you want to switch this feature off and make all indentation, for example, two spaces, you'd do the following in your User Settings or Workspace settings. 如果要关闭此功能并进行所有缩进(例如,两个空格),则需要在“用户设置”或“工作区”设置中执行以下操作。
{
"editor.tabSize": 2,
"editor.detectIndentation": false
}
#5楼
We can control tab size by file type with EditorConfig and its Visual Studio Code extension . 我们可以使用EditorConfig及其Visual Studio Code扩展来 按文件类型控制选项卡大小 。 We then can make Alt + Shift + F specific to each file type. 然后,我们可以使Alt + Shift + F特定于每种文件类型。
Installation 安装
ext install EditorConfig
Example Configuration 示例配置
.editorconfig .editorconfig
[*]
indent_style = space
[*.{cs,js}]
indent_size = 4
[*.json]
indent_size = 2
settings.json settings.json
EditorConfig overrides whatever settings.json configures for the editor. EditorConfig会覆盖编辑器的所有settings.json配置。 There is no need to change editor.detectIndentation
. 无需更改editor.detectIndentation
。
#6楼
您希望确保您的editorconfig与您的用户或工作区设置配置不冲突,因为我只是有点烦恼,认为当我的编辑器配置撤消这些更改时,没有应用设置文件设置。