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

263

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

我想关闭自动转换,并确保我的项目只有制表符。

编辑:

通过此设置,HTML文件似乎可以正常工作,但TypeScript文件不行。

{
  "editor.insertSpaces": false
}

45
我不明白为什么IDE默认将制表符转换为空格。如果我想要空格,我会像野蛮人一样按空格键四次来缩进。请注意,这只是翻译,没有解释或其他内容。 - Joshua Schlichting
这个回答解决了你的问题吗?如何在现有文件中将制表符转换为空格,反之亦然? - undefined
13个回答

442

.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
Ln44, Col .  [Spaces:4] . UTF-8 with BOM . CTRLF . HTML . :)

编辑:
要将现有缩进从空格转换为制表符,请按Ctrl+Shift+P,然后键入:

>Convert indentation to Tabs

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


5
我该如何在我的工作区中对所有文件运行这个命令? - Vijender Kumar
12
在VSCode中,如何永久地将缩进转换为制表符,以便适用于所有文件和项目? - Farid Alijani
1
@FäridAlijani,@VijenderKumar 我认为使用VS Code不可能实现。 - v-andrew

184
为更改选项卡设置,请单击位于vscode窗口右下角状态栏中Ln/Col文本的文本区域。
名称可以是“制表符大小”或“空格”。
将弹出一个菜单,其中包含所有可用的操作和设置。
图片请查看链接:enter image description here

5
对于我而言,这只会改变我当前正在查看的特定文件的设置。项目中的其他文件保留其旧设置。如何将它们普遍地设置? - Kyle Vassella
通常情况下,如果需要的话,我会一次性替换所有文件,通过使用简单的正则表达式 {4}进行项目搜索,并用\t(制表符)进行替换。通常还会浏览匹配项,以确保不会出现多行字符串字面值等问题。显然,应用这些设置,使您的新缩进也是所需的字符! - cossacksman

58

Ctrl+Shift+P,然后选择 "将缩进转换为制表符"


1
这是否可以针对选择而不是整个文件进行操作? - dbrane
1
能否对整个代码库进行操作,而不仅仅是一个文件? - Matt Wyndham
@dbrane 是的。如果您在文件中突出显示一块代码,则转换仅适用于所选内容。顺便说一下,在 Mac 上,组合键为⌥ + P(alt/option + P)。 - Trutane

36

如果你想使用选项卡而不是空格

请尝试以下操作:

  1. 前往文件首选项设置 或者直接按下 Ctrl + ,
  2. 在顶部的搜索设置栏中插入 editor.insertSpaces
  3. 你会看到类似这样的内容:编辑器:插入空格,它可能已经被选中。只需像下面的图片所示取消选择即可。

Editor: Insert Spaces

  1. 重新加载Visual Studio Code(按下F1 ➤ 输入 reload window ➤ 按下 Enter

如果上述方法不起作用,请尝试以下操作:

这可能是由于安装了插件JS-CSS-HTML Formatter导致的。

(你可以通过前往文件首选项扩展 或者直接按下 Ctrl + Shift + X 检查,你将在启用的列表中找到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. 保存它(前往 文件保存 或者只需按下 Ctrl + S
  2. 重新加载Visual Studio Code(按下 F1 ➤ 输入 reload window ➤ 按下 Enter

如果您想要用空格代替制表符,请修改 formatter.json 文件:在任何引号中放入一个 _空格_,而不是 \t (所以 " \t " 变成了 " "),并在看到 1 的地方放置 4。因此您的文件可能会像这样: "indent_size": 4, "indent_char": " " "indent_size": 4, "indentCharacter": " ", "indent_char": " ", "indent_size": 4, "indent_char": " ", "indent_character": " " - Alex Logvin
2
重新加载窗口步骤是我所缺少的。很令人沮丧的是,当我只需要重新加载窗口时,我必须进行三次勾选框和调整设置的过程,却没有任何改变发生。感谢你的提示! - Rin and Len
1
重新加载 VS Code 就解决了问题。 - DemiA
1
谢谢。 - Alex Sham

10

请查看官方的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
}

9
  1. 突出显示您的代码(在文件中)
  2. 点击应用程序窗口右下角的Tab Size Tab Size in bottom righthand corner of application window image
  3. 选择适当的"转换缩进为制表符" Select the appropriate Convert Indentation to Tabs image

9
在我的情况下,问题出在一月更新后安装的 JS-CSS-HTML 格式化 扩展上。默认的 indent_char 属性是空格。我卸载了它,奇怪的行为就停止了。

哦,是的,这正是我的实际问题。我花了很多时间在 VS Code 的设置上摆弄。最终禁用此扩展解决了问题。 - Avijeet

9
以下设置对我很有效:
"editor.insertSpaces": false,
"editor.formatOnSave": true, // only if you want auto fomattting on saving the file
"editor.detectIndentation": false

上述设置将反映并应用于每个文件。您不需要手动缩进/格式化每个文件。

6
-> -> 或者按下Ctrl+,并搜索,然后取消选择此选项:

enter image description here

我不得不重新打开文件以使更改生效。

2

如果你使用 .sass 文件出现了 Expected tabs, was spaces 错误,请将此设置为 false

"editor.detectIndentation": false

...然后选择你的代码块,按下tab键进行缩进,再按下shift + tab键进行反缩进。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接