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

Visual Studio Code - Convert spaces to tabs

我的项目中有 TypeScript 和 HTML 文件,在这两个文件中,制表符都转换为空格。

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

编辑:

使用此设置似乎可以在 HTML 文件中使用,但不能在 TypeScript 文件中使用。

{
  "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 从您的文件中检测到它,您必须禁用它。 如果没有帮助,请检查您是否没有具有更高优先级的设置。 例如,当您将其保存到用户设置时,它可能会被项目文件夹中的工作区设置覆盖。

更新:

要访问这些设置,您可以打开 文件 » 首选项 » 设置,单击管理 齿轮图标在左下方,或使用键盘快捷键:

CTRL+,(Windows,Linux)

+,(Mac)

更新:

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

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

>Convert indentation to Tabs

这将根据定义的设置将文档的缩进更改为制表符。

要更改选项卡设置,请单击 vscode window 右下角状态栏中 Ln/Col 文本右侧的文本区域。

名称可以是Tab SizeSpaces

将弹出一个菜单,其中包含所有可用的操作和设置。

就我而言,问题是在 1 月更新后安装了 JS-CSS-HTML 格式化程序 扩展。默认的 indent_char 属性 是 space。我卸载了它,奇怪的行为停止了。

以下设置对我来说效果很好,

"editor.insertSpaces": false,
"editor.formatOnSave": true, // only if you want auto fomattting on saving the file
"editor.detectIndentation": false

以上设置将反映并应用到每个文件。您不需要手动 indent/format 每个文件。

Ctrl+Shift+P,然后"Convert Indentation to Tabs"

从官方 vscode 设置中检查:

// Controls whether `editor.tabSize#` and `#editor.insertSpaces` will be automatically detected when a file is opened based on the file contents.
"editor.detectIndentation": true,

// The number of spaces a tab is equal to. This setting is overridden based on the file contents when `editor.detectIndentation` is on.
"editor.tabSize": 4,

// Config the editor that making the "space" instead of "tab"
"editor.insertSpaces": true,

// Configure editor settings to be overridden for [html] language.
"[html]": {
    "editor.insertSpaces": true,
    "editor.tabSize": 2,
    "editor.autoIndent": false
}

如果您想使用制表符而不是空格

试试这个:

  1. 转到FilePreferencesSettings 或者直接按 Ctrl + ,
  2. 搜索设置顶部的栏中插入editor.insertSpaces
  3. 您会看到类似这样的内容:编辑器:插入空格,它可能会被检查。只需取消选中它,如下图所示

  1. 重新加载 Visual Studio 代码(按 F1 ➤ 输入 reload window ➤ 按 输入)

如果不起作用,试试这个:

可能是因为安装了插件JS-CSS-HTML Formatter

(你可以去FilePreferencesExtensions 或者直接按 Ctrl + Shift + X, 在 Enabled 列表中你会发现 JS-CSS-HTML Formatter)

如果是这样你可以修改这个插件:

  1. F1 ➤ 输入Formatter config ➤ 按Enter(它将打开文件formatter.json
  2. 像这样修改文件:
 4|    "indent_size": 1,
 5|    "indent_char": "\t"
——|
24|    "indent_size": 1,
25|    "indentCharacter": "\t",
26|    "indent_char": "\t",
——|
34|    "indent_size": 1,
35|    "indent_char": "\t",
36|    "indent_character": "\t"
  1. 保存(转到 FileSave 或只需按 Ctrl + S)
  2. 重新加载Visual Studio代码(按F1 ➤ 输入reload window ➤ 按输入)

文件 -> 首选项 -> 设置 或直接按 Ctrl + , 并搜索 spaces,然后停用此选项:

我不得不重新打开文件,这样更改才会生效。

  1. 突出显示您的代码(在文件中)
  2. 单击应用程序右下角的标签大小 window
  3. Select适当的将缩进转换为制表符
{
  "editor.insertSpaces": true
}

True 适合我。

如果您想在很多文件中将制表符更改为空格,但又不想单独打开它们,我发现使用 查找和替换同样有效 最左侧工具栏中的选项。

在第一个框 (Find) 中,复制并粘贴源代码中的一个选项卡。

在第二个框 (Replace) 中,输入您希望使用的空格数(即 2 或 4)。

如果您按下 ... 按钮,您可以指定要包含或忽略的目录(即 src/Data/Json)。

最后,检查结果预览并按全部替换。工作区中的所有文件都可能受到影响。

如果您正在使用 .sass 文件,请将此设置为 false,并且它会给您 Expected tabs, was spaces 错误:

"editor.detectIndentation": false

... 然后 select 您的代码块并按 tab 键缩进,然后按 shift + tab 键缩进。

在我的例子中,它是关于取消选中的

更漂亮:使用标签

VSCode 设置