使用 Eclipse 用制表符替换空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22213098/
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
Replace spaces with tabs using Eclipse
提问by Joshua Hyatt
I downloaded some code from the Internet, and the four-space indentation is bugging the crap out of me. I tried to do a find and replace for "<4 spaces>" and replace it with "\t", but that just replaced all sets of spaces with the string "\t". How would I put a tab in the "replace" box?
我从 Internet 上下载了一些代码,四个空格的缩进让我很烦。我尝试查找并替换“<4 个空格>”并将其替换为“\t”,但这只是将所有空格集替换为字符串“\t”。我如何在“替换”框中放置一个选项卡?
回答by Kohei Mikami
- Open Window->Preferences from menu bar.
- Select
Text Editors
from tree menu. - Uncheck
Insert spaces for tabs
.
- 从菜单栏中打开窗口-> 首选项。
- 选择
Text Editors
从树形菜单。 - 取消选中
Insert spaces for tabs
。
After that, run Format
menu from context menu and save the file:
之后,Format
从上下文菜单运行菜单并保存文件:
回答by Joshua Hyatt
I figured it out. I just check the box that says "use regular expressions", then use \t.
我想到了。我只是选中“使用正则表达式”框,然后使用 \t。
回答by Caleb
In answer to the second half of your question:
回答你问题的后半部分:
The reason why people like spaces more than tabs is consistency. If you have your editor set to show tabs as 4 spaces wide, and I have my editor set to show tabs as 8 spaces wide, the code we're writing will look different to each of us. That's okay until our mutual coworker forgets that we're using tabs and starts spacing his lines using 4 spaces. Now his code looks fine to you, but all the indentation is off to me.
人们喜欢空格多于制表符的原因是一致性。如果您将编辑器设置为将选项卡显示为 4 个空格宽,而我将我的编辑器设置为将选项卡显示为 8 个空格宽,那么我们编写的代码对我们每个人来说都会有所不同。没关系,直到我们共同的同事忘记我们正在使用制表符并开始使用 4 个空格分隔他的行。现在他的代码对你来说看起来不错,但对我来说所有的缩进都没有。
Also, what happens when our work decides that lines should be 80 characters long, or 120 characters long? You'll happily code with 4 space tabs, and when it gets to my editor, I suddenly see some of your lines as too long.
另外,当我们的工作决定行应该是 80 个字符长还是 120 个字符长时会发生什么?你会很高兴地用 4 个空格选项卡编写代码,当它到达我的编辑器时,我突然看到你的一些行太长了。
Generally, it doesn't matter whether you use spaces or tabs, as long as you (and every person who works on your code) agrees on which to use, and how wide a tab character is.
通常,使用空格还是制表符并不重要,只要您(以及每个处理您的代码的人)同意使用哪个以及制表符的宽度。
I use 4 spaces everywhere, because it looks the same on every editor, every repository, when cat
ed, and everywhere else.
我在任何地方都使用 4 个空格,因为它在每个编辑器、每个存储库、何时cat
ed 和其他任何地方看起来都是一样的。
回答by Emmanuel Oga
Kouhei response is in the right track, but you'll need to change the options of the Java formatter if you want the auto formatter to use spaces (in the preferences, look for Java-> Code Style-> Formatter, create a new style from one of the existing one, and choose "always use spaces"). Then, Ctrl+ Shift+ Fwill remove the tabs and insert spaces.
Kouhei 的响应是正确的,但是如果您希望自动格式化程序使用空格,则需要更改 Java 格式化程序的选项(在首选项中,查找Java-> Code Style-> Formatter,创建一个新样式从现有的其中之一,然后选择“始终使用空格”)。然后,Ctrl+ Shift+F将删除的选项卡和插入空格。
回答by Sandeep Kumar
To resolve the issue of tab with space in the Eclipse editor:
要解决 Eclipse 编辑器中带有空格的制表符问题:
Menu Window--> Preferences--> Java--> Code style--> Formatter
Click on configure project specific settings.
- Choose the project
Check Enable project-specific settings
- Click on New
- Mention your profile name --> click OK.
The profile page will be popped up
- Choose Space onlyunder tab policy label of the indentation tab.
- Click apply and the OK.
Use Ctrl+ Shift+ Fto format a Java class which will replace tab with space.
菜单窗口-->首选项--> Java-->代码样式-->格式化程序
单击配置项目特定设置。
- 选择项目
选中启用项目特定的设置
- 点击新建
- 提及您的个人资料名称 --> 单击OK。
将弹出个人资料页面
- 在缩进选项卡的选项卡策略标签下选择仅空格。
- 单击应用和OK。
使用Ctrl+ Shift+F格式化一个Java类,这将空格替换选项卡。