Visual Studio Code - 将空格转换为制表符

问题描述:

我的项目中有 TypeScript 和 HTML 文件,两个文件中的选项卡都转换为空格.

I have both TypeScript and HTML files in my project, in both files tabs are converted to spaces.

我想关闭自动转换并确保我的项目只有标签.

I want to turn the auto-conversion off and make sure that my project has only tabs.

使用此设置,它似乎适用于 HTML 文件,但不适用于 TypeScript 文件.

With this setting it seems to work in HTML files but not in TypeScript files.

{
  "editor.insertSpaces": false
}

.vscode/settings.json有3个选项:

// The number of spaces a tab is equal to.
"editor.tabSize": 4,

// Insert spaces when pressing Tab.
"editor.insertSpaces": true,

// When opening a file, `editor.tabSize` and `editor.insertSpaces` will be detected based on the file contents.
"editor.detectIndentation": true

editor.detectIndentation 从您的文件中检测到它,您必须禁用它.如果没有帮助,请检查您是否没有具有更高优先级的设置.例如,当您将其保存到用户设置时,它可能会被项目文件夹中的工作区设置覆盖.

editor.detectIndentation detects it from your file, you have to disable it. If it didn't help, check that you have no settings with higher priority. For example when you save it to User settings it could be overwritten by Workspace settings which are in your project folder.

更新:

您可以直接打开文件 » 首选项 » 设置或使用快捷方式:

You may just open File » Preferences » Settings or use shortcut:

CTRL+ (Windows、Linux)

+, (Mac)

更新:

现在您可以选择手动编辑这些选项.
点击编辑器右下角的选择器 Spaces:4:

Now you have alternative to editing those options manually.
Click on selector Spaces:4 at the bottom-right of the editor:

如果要将现有 ws 转换为选项卡,请从 Marketplace 安装扩展

要将现有缩进从空格转换为制表符,请按 Ctrl+Shift+P 并输入:

When you want to convert existing ws to tab, install extension from Marketplace

To convert existing indentation from spaces to tabs hit Ctrl+Shift+P and type:

>Convert indentation to Tabs

这将根据已定义的选项卡设置更改文档的缩进.

This will change the indentation for your document based on the defined settings to Tabs.