typescript 将空格转换为制表符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36814642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:05:24  来源:igfitidea点击:

Convert spaces to tabs

typescriptvisual-studio-codetabsspaces

提问by Matan Yedayev

I'm writing TypeScriptand HTMLfiles, and tabs gets converted to spaces.

我正在编写TypeScriptHTML文件,制表符被转换为空格。

In my TypeScriptproject, every tab converts to spaces, I want to turn it off, and have a tab instead of spaces.

在我的TypeScript项目中,每个制表符都转换为空格,我想将其关闭,并使用制表符而不是空格。

This is my settings:

这是我的设置:

{
  "editor.insertSpaces": false
}

EDIT 1:

编辑 1:

It seems to work in .htmlfiles, but not in .tsfiles.

它似乎适用于.html文件,但不适用于.ts文件。

回答by v-andrew

There are 3 options in .vscode/settings.json:

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

// 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.detectIndentationdetects 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.

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

Update:

更新:

You may just open File? Preferences? Settingsor use shortcut:

您可以只打开File吗?偏好设置或使用快捷方式:

CTRL+,(Windows, Linux)

CTRL+ ,(Windows、Linux)

?+,(Mac)

?+ ,(Mac)

Update:

更新:

Now you have alternative to editing those options manually.
Click on selector Spaces:4at the bottom-right of the editor:
 Ln44, Col .  [Spaces:4] . UTF-8 with BOM . CTRLF . HTML . :)

现在您可以替代手动编辑这些选项。
单击编辑器右下角的选择器Spaces:4
Ln44,上校。 [空格:4]。 带有 BOM 的 UTF-8 。 CTRLF 。 HTML 。 :)

When you want to convert existing ws to tab, install extension from Marketplace
EDIT:
To convert existing indentation from spaces to tabs hit Ctrl+Shift+Pand type:

如果要将现有 ws 转换为选项卡,请从 Marketplace 安装扩展
编辑:
要将现有的缩进从空格转换为制表符,请点击Ctrl+ Shift+P并键入:

>Convert indentation to Tabs

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

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

回答by Eric Bole-Feysot

To change tab settings, click the text area right to the Ln/Col text in the status bar on the bottom right of vscode window.

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

The name can be Tab Sizeor Spaces.

名称可以是Tab SizeSpaces

A menu will pop up with all available actions and settings.

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

enter image description here

在此处输入图片说明

回答by carlsborg

Ctrl+Shift+P, then "Convert Indentation to Tabs"

Ctrl+ Shift+ P,然后“将缩进转换为制表符”

回答by Alex Logvin

If you want to use tabs instead of spaces

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

Try this:

尝试这个:

  1. Go to File? Preferences? Settingsor just press Ctrl+,
  2. In the Search settingsbar on top insert editor.insertSpaces
  3. You will see something like this: Editor: Insert Spacesand it will be probably checked. Just uncheck it as show in image below
  1. FilePreferences? Settings或只需按Ctrl+,
  2. 在顶部插入的搜索设置栏中editor.insertSpaces
  3. 你会看到这样的东西:编辑器:插入空格,它可能会被选中。只需取消选中它,如下图所示

Editor: Insert Spaces

编辑器:插入空格

  1. Reload Visual Studio Code(Press F1? type reload window? press Enter)
  1. 重新加载Visual Studio 代码(按F1?输入reload window?按Enter


If it doesn't worked try this:

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

It's probably because of installed plugin JS-CSS-HTML Formatter

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

(You can check it by going to File? Preferences? Extensionsor just pressing Ctrl+Shift+X, in the Enabledlist you will find JS-CSS-HTML Formatter)

(您可以通过转到File? Preferences?Extensions或直接按Ctrl+ Shift+来检查它X,在启用列表中您会找到JS-CSS-HTML Formatter

If so you can modify this plugin:

如果是这样,您可以修改此插件:

  1. Press F1? type Formatter config? press Enter(it will open the file formatter.json)
  2. Modify the file like this:
  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. Save it (Go to File? Saveor just press Ctrl+S)
  2. Reload Visual Studio Code(Press F1? type reload window? press Enter)
  1. 保存(转到File?Save或只需按Ctrl+S
  2. 重新加载Visual Studio 代码(按F1?输入reload window?按Enter

回答by Vinicios Torres

In my case, the problem was JS-CSS-HTML Formatterextension installed after january update. The default indent_charproperty is space. I uninstalled it and the weird behavior stops.

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

回答by Rohan Kumar

Below settings are worked well for me,

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

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

Above settings will reflect and applied to every files. You don't need to indent/format every file manually.

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

回答by Xin

Check this from official vscode setting:

从官方 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
}

回答by lama12345

File-> Preferences-> Settingsor just press Ctrl+ ,and search for spaces, then just deactivate this option:

File-> Preferences-> Settings或只需按Ctrl+,并搜索空格,然后停用此选项:

enter image description here

在此处输入图片说明

I had to reopen the file so the changes would take effect.

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

回答by RealScatman

  1. Highlight your Code (in file)
  2. Click Tab Size in bottom righthand corner of application window Tab Size in bottom righthand corner of application window image
  3. Select the appropriate Convert Indentation to Tabs Select the appropriate Convert Indentation to Tabs image
  1. 突出显示您的代码(在文件中)
  2. 单击应用程序窗口右下角的 Tab Size 应用程序窗口图像右下角的标签大小
  3. 选择适当的将缩进转换为制表符 选择适当的将缩进转换为制表符图像

回答by ilovethedrama

{
  "editor.insertSpaces": true
}

Trueworks for me.

True对我来说有效。