如何更改 Eclipse 以使用空格而不是制表符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/407929/
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-10 18:34:22  来源:igfitidea点击:

How do I change Eclipse to use spaces instead of tabs?

eclipseformattingtabswhitespace

提问by Brian Deacon

By default Eclipseindents with a hard tab character. How do I change it to spaces?

默认情况下,Eclipse使用硬制表符缩进。如何将其更改为空格?

回答by Dave Ray

Java Editor

Java 编辑器

  1. Click Window? Preferences
  2. Expand Java? Code Style
  3. Click Formatter
  4. Click the Editbutton
  5. Click the Indentationtab
  6. Under General Settings, set Tab policyto: Spaces only
  7. Click OKad nauseam to apply the changes.
  1. 单击窗口? 喜好
  2. 展开Java? 代码风格
  3. 单击格式化程序
  4. 单击编辑按钮
  5. 单击缩进选项卡
  6. General Settings 下,将Tab policy设置为:Spaces only
  7. 单击“确定”以应用更改。

[Note: If necessary save profile with a new name as the default profile cannot be overwritten.]

[注意:如有必要,使用新名称保存配置文件作为默认配置文件不能被覆盖。]

Default Text Editor

默认文本编辑器

Before version 3.6:

3.6 版之前:

Window->Preferences->Editors->Text Editors->Insert spaces for tabs

Version 3.6 and later:

3.6 及更高版本:

  1. Click Window? Preferences
  2. Expand General? Editors
  3. Click Text Editors
  4. Check Insert spaces for tabs
  5. Click OKad nauseam to apply the changes.
  1. 单击窗口? 喜好
  2. 展开一般? 编辑
  3. 单击文本编辑器
  4. 检查为制表符插入空格
  5. 单击“确定”以应用更改。

Note that the default text editor is used as the basis for many non-Java editors in Eclipse. It's astonishing that this setting wasn't available until 3.3.

请注意,默认文本编辑器用作 Eclipse 中许多非 Java 编辑器的基础。令人惊讶的是,此设置直到 3.3 才可用。

C / C++

C/C++

  1. Click Window? Preferences
  2. Expand C/C++? Code Style
  3. Click Formatter
  4. Click the Newbutton to create a new profile, then OKto continue
  5. Click the Indentationtab
  6. Under General Settings, set Tab policyto: Spaces only
  7. Click OKad nauseam to apply the changes.
  1. 单击窗口? 喜好
  2. 展开C/C++? 代码风格
  3. 单击格式化程序
  4. 单击“新建”按钮创建新配置文件,然后单击“确定”继续
  5. 单击缩进选项卡
  6. General Settings 下,将Tab policy设置为:Spaces only
  7. 单击“确定”以应用更改。

HTML

HTML

  1. Click Window? Preferences
  2. Expand Web? HTML Files
  3. Click Editor
  4. Under Formatting, select the Indent using spacesradio button
  5. Click OKto apply the changes.
  1. 单击窗口? 喜好
  2. 展开网络? HTML 文件
  3. 单击编辑器
  4. Formatting 下,选择Indent using Spaces单选按钮
  5. 单击确定以应用更改。

CSS

CSS

Follow the same instructions for HTML, but select CSS Filesinstead of HTML Files.

遵循 HTML 的相同说明,但选择CSS Files而不是HTML Files

JSP

JSP

By default, JSP files follow the formatting preferences for HTML Files.

默认情况下,JSP 文件遵循 HTML 文件的格式设置首选项。

XML

XML

XML files spacing is configured in Preferences.

XML 文件间距在首选项中配置。

  1. Click Window? Preferences
  2. Expand XML? XML Files
  3. Click Editor
  4. Select Indent using spaces
  5. You can specify the Indentation sizeif needed: number of spaces to indent.
  1. 单击窗口? 喜好
  2. 展开XML? XML 文件
  3. 单击编辑器
  4. 选择使用空格缩进
  5. 如果需要,您可以指定缩进大小:要缩进的空格数。

回答by Patrick Fisher

For the default text editor:

对于默认文本编辑器:

  • General? Editors? Text Editors? Insert spaces for tabs(check it)
  • 将军编辑文本编辑器? Insert spaces for tabs(核实)


For PHP:

对于 PHP:

  • PHP? Code Style? Formatter? Tab policy(choose "spaces")
  • PHP? Code Style? Formatter? Indentation size(set to 4)
  • PHP代码风格格式化程序Tab policy(选择“空格”)
  • PHP代码风格格式化程序Indentation size(设置为 4)


For CSS:

对于 CSS:

  • Web? CSS? Editor? Indent using spaces(select it)
  • Web? CSS? Editor? Indentation size(set to 4)
  • CSS? 编辑Indent using spaces(选择它)
  • CSS? 编辑Indentation size(设置为 4)


For HTML:

对于 HTML:

  • Web? HTML? Editor? Indent using spaces(select it)
  • Web? HTML? Editor? Indentation size(set to 4)
  • HTML? 编辑Indent using spaces(选择它)
  • HTML? 编辑Indentation size(设置为 4)


For XML:

对于 XML:

  • XML? XML Files? Editor? Indent using spaces(select it)
  • XML? XML Files? Editor? Indentation size(set to 4)
  • XML? XML 文件? 编辑Indent using spaces(选择它)
  • XML? XML 文件? 编辑Indentation size(设置为 4)


For Javascript:

对于 JavaScript:

  • Javascript? Preferences? Code Style? Formatter? Edit? Indentation(choose "spaces only")
  • Rename the formatter settings profile to save it
  • Javascript偏好代码风格格式化程序编辑Indentation(选择“仅限空格”)
  • 重命名格式化程序设置配置文件以保存它


For Java:

对于 Java:

  • Java? Preferences? Code Style? Formatter? Edit? Indentation(choose "spaces only")
  • Rename the formatter settings profile to save it
  • 爪哇偏好代码风格格式化程序编辑Indentation(选择“仅限空格”)
  • 重命名格式化程序设置配置文件以保存它

回答by Brian Deacon

From changing tabs to spaces in eclipse:

在 Eclipse 中将制表符更改为空格

Window? Preferences? Java? Code Style? Formatter? Edit? Indentation(choose "Spaces Only")

窗户偏好爪哇代码风格格式化程序编辑Indentation(选择“仅空格”)

回答by Antonio Leite

For CDT:

对于CDT

  • Go toWindow/Preference? C/C++? Code Style? Formatter? New
  • Create a new one because the built in profile can not be changed
  • MyProfile(choose one name for the profile)
  • Indentation? Tab Policy(choose Spaces only)
  • 转到窗口/首选项C/C++? 代码风格格式化程序新的
  • 创建一个新的,因为无法更改内置配置文件
  • MyProfile (为配置文件选择一个名称)
  • 缩进? Tab Policy(仅选择空格)

回答by Nick Chadwick

Just a quick tip for people stumbling across this thread; there is one more placewhere this setting can also be set, in your project!

只是给在这个线程上绊倒的人的一个快速提示;还有一个地方也可以设置这个设置,在你的项目中!

Eclipse supports project-specific settings, and some projects will use their own, un-managed tabs/spaces settings, which won't show up anywhere except the current project Properties.

Eclipse 支持特定于项目的设置,并且一些项目将使用自己的、未管理的选项卡/空格设置,除了当前项目的属性之外不会出现在任何地方。

This can be managed through:

这可以通过以下方式进行管理:

  • Right-Click current Project in Package Explorer
  • Properties? Java Code Style
  • Turn offall the project-specific options
  • 包资源管理器中右键单击当前项目
  • 属性Java 代码风格
  • 关闭关闭所有的项目特定选项

This will generally only be an issue if you import someone else's code into your Eclipse.

这通常只有在您将其他人的代码导入 Eclipse 时才会出现问题。

回答by Sumit Singh

For Default Editor:

对于默认编辑器:

Window? Preferences? Editors? Text Editors? Insert spaces for tabs

窗户偏好编辑文本编辑器?Insert spaces for tabs

enter image description here

在此处输入图片说明

For Java editor

对于 Java 编辑器

Window? Preferences? Java? Code Style? Formatter? Edit? Indentation? Tab policy"Spaces Only"

窗户偏好爪哇代码风格格式化程序编辑缩进? Tab policy“只有空格”

enter image description here

在此处输入图片说明

回答by user1978019

When I faced this problem I had "use spaces for tabs" set to true everywhere I could find, and yet I was still getting tabs. It ended up being because tabs were used elsewhere in the file and it was trying to do smart indentation or something frustrating.

当我遇到这个问题时,我在我能找到的任何地方都将“使用制表符空格”设置为 true,但我仍然得到制表符。它最终是因为在文件的其他地方使用了制表符,并且它试图进行智能缩进或令人沮丧的事情。

It was resolved by selecting the entire contents of the file and pressing, on a mac, command+shift+f. This applies the given formatting to a file. I do not know the same keybinding on windows, but give that a try. At that point I begin getting the expected behavior.

通过选择文件的全部内容并在 Mac 上按command+shift+f. 这将给定的格式应用于文件。我不知道 Windows 上的相同键绑定,但请尝试一下。那时我开始得到预期的行为。

回答by Khokhar

Eclipse IDE for C/C++ Developers, Version: Helios Service Release 2

面向 C/C++ 开发人员的 Eclipse IDE,版本:Helios Service Release 2

You need to create new profile by pressing New button inside "Window->Preferences->Code Style"

您需要通过按“窗口->首选项->代码样式”中的“新建”按钮来创建新配置文件

Go to Indentation tab and select "Tab policy = Space only"

转到缩进选项卡并选择“Tab policy = Space only”



Eclipse IDE for C/C++ Developers, Version: Kepler Service Release 1

面向 C/C++ 开发人员的 Eclipse IDE,版本:Kepler Service Release 1

Follow the path below to create new profile: "Window > Preferences > C/C++ > Code Style > Formatter"

按照下面的路径创建新的配置文件:“窗口 > 首选项 > C/C++ > 代码样式 > 格式化程序”

Go to Indentation tab and select "Tab policy = Space only"

转到缩进选项卡并选择“Tab policy = Space only”

回答by Micha? Orliński

I found the solution this problem very simple and which works always. It is change the eclipse setting file.

我发现这个问题的解决方案非常简单,而且总是有效。它是更改eclipse设置文件。

For example (change HTML indentation size):

例如(更改 HTML 缩进大小):



  1. Found org.eclipse.wst.html.core.prefsfile which should be in your_workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/
  2. Add/Change to line in file:
  1. 找到org.eclipse.wst.html.core.prefs应该在的文件your_workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/
  2. 添加/更改为文件中的行:
indentationChar=space

indentationSize=4

回答by ricardo rivaldo

Be sure to check the java formater since it overwrites the "insert spaces for tabs"setting. Go to:

请务必检查 java 格式化程序,因为它会覆盖“为制表符插入空格”设置。去:

Java->Code Style"->Formatter->Edit->Identation

Java->Code Style"->Formatter->Edit->Identation

Note: you will need to create a custom format to be able to save your configuration.

注意:您需要创建自定义格式才能保存配置。