如何自动删除 Eclipse 中的尾随空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1043433/
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
How to auto-remove trailing whitespace in Eclipse?
提问by Jan ?ankowski
The question has two parts, one of which I already have the answer for.
这个问题有两个部分,其中一个我已经有了答案。
- How to auto-remove trailing whitespace from the entire file being edited? -> Answer: use the AnyEdit plugin, which can be set to do that on any save to the file.
- How to auto-remove trailing whitespace only from the lines I changed? -> This I don't know and would appreciate any help.
- 如何从正在编辑的整个文件中自动删除尾随空格?-> 答案:使用AnyEdit 插件,它可以设置为在任何保存到文件时执行此操作。
- 如何仅从我更改的行中自动删除尾随空格?-> 这个我不知道,希望得到任何帮助。
采纳答案by Dan Berindei
- Removing whitespace from the entire file being edited:
- 从正在编辑的整个文件中删除空格:
Preferences-> Java-> Editors-> Save Actions-> check Perform the selected actions on save-> check Additional actions-> click Configure..-> go to Code organizingtab -> check Remove trailing whitespace-> select All lines.
Preferences-> Java-> Editors-> Save Actions-> check Perform the selected actions on save-> check Additional actions-> click Configure..-> go to Code organizingtab -> check Remove trailing whitespace-> select All lines。
- Removing whitespace only from the lines I changed:
- 仅从我更改的行中删除空格:
Preferences-> Java-> Editor-> Save Actions-> check "Perform the selected actions on save-> check Format source code-> select Format edited lines.
Preferences-> Java-> Editor-> Save Actions-> 检查“ Perform the selected actions on save-> 检查Format source code-> 选择Format edited lines。
Note it is not necessary to click Configure the formatter settings on the Formatter page- all the code styles automatically include removing trailing whitespace. At least I couldn't find a setting for this in Formatter configuration, and it worked out of the box for built-in Java Conventions, Eclipse, Eclipse 2.1 styles, as well as GoogleStyle.
请注意,没有必要单击Configure the formatter settings on the Formatter page- 所有代码样式都会自动包括删除尾随空格。至少我在 Formatter 配置中找不到这个设置,而且它对于内置的 Java 约定、Eclipse、Eclipse 2.1 样式以及GoogleStyle 都是开箱即用的。
When using this set-up, you obviously need to also turn offthe solution to part 1 of the question.
使用此设置时,您显然还需要关闭问题第 1 部分的解决方案。
Eclipse version checked: 4.5.2, 4.11
回答by gcastro
I assume your questions is with regards to Java code. If that's the case, you don't actually need any extra plugins to accomplish 1). You can just go to Preferences -> Java -> Editor -> Save Actionsand configure it to remove trailing whitespace.
我假设您的问题与 Java 代码有关。如果是这种情况,您实际上不需要任何额外的插件来完成 1)。您只需转到Preferences -> Java -> Editor -> Save Actions并将其配置为删除尾随空格。
By the sounds of it you also want to make this a team-wide setting, right? To make life easier and avoid having to remember setting it up every time you have a new workspace you can set the save action as a project specific preference that gets stored into your SCM along with the code.
听上去你也想让这成为一个团队范围的设置,对吧?为了让生活更轻松并避免每次拥有新工作区时都要记住设置它,您可以将保存操作设置为项目特定的首选项,该首选项与代码一起存储到 SCM 中。
In order to do that right-click on your project and go to Properties -> Java Editor -> Save Actions. From there you can enable project specific settings and configure it to remove trailing whitespace (among other useful things).
为此,右键单击您的项目并转到Properties -> Java Editor -> Save Actions。从那里你可以启用项目特定的设置并配置它以删除尾随空格(以及其他有用的东西)。
NB: This option has been removed in Eclipse Kepler (4.3) and following releases.
注意:此选项已在 Eclipse Kepler (4.3) 和后续版本中删除。
NB #2:The option seems to be back in Eclipse Luna - Luna Service Release 1a (4.4.1)
注意 #2:该选项似乎又回到了 Eclipse Luna -Luna Service Release 1a (4.4.1)
回答by Dave
You don't need any plugin to do so. For instance, if you code JAVA, you can erase trailing whitespaces configuring save actions:
你不需要任何插件来做到这一点。例如,如果您编写 JAVA 代码,您可以擦除配置保存操作的尾随空格:
Eclipse 3.6
日食 3.6
Preferences-> Java-> Editors-> Save Actions-> Check Perform the selected actions on save-> Check Additional actions-> Click the Configure..button.
Preferences-> Java-> Editors-> Save Actions-> 检查Perform the selected actions on save-> 检查Additional actions-> 点击Configure..按钮。
In the Code organizingtab, check Remove trailing whitespace
在Code organizing选项卡中,检查Remove trailing whitespace
回答by Ciantic
PyDevcan do it by either Ctrl+Shift+Fif you have code formatter option set to do it, or by during saving:
PyDev的可以做,要么Ctrl+ Shift+ F,如果你有代码格式化选项设置为做到这一点,或者通过during saving:
Eclipse-> Window-> Preferences-> PyDev-> Editor-> Code Style-> Code Formatter:
Eclipse-> Window-> Preferences-> PyDev-> Editor-> Code Style-> Code Formatter:
I use at least these:
我至少使用这些:
- Auto format before saving
- Right trim lines?
- Add new line at end of file
- 保存前自动格式化
- 正确的修剪线?
- 在文件末尾添加新行
回答by Sumit Singh
Do following:
执行以下操作:
Preferences-> Java-> Editor-> Save Actions
Preferences-> Java-> Editor->Save Actions
回答by Kena
You can map a key in Eclipse to manually remove trailing whitespaces in the whole file, but only on request instead of automatically at save. (Preference/Keys and then map a set of keys to File/Remove Trailing Whitespace) This can be useful if you want to sanitize all new files, but keep legacy code untouched.
您可以在 Eclipse 中映射一个键来手动删除整个文件中的尾随空格,但只能根据请求而不是在保存时自动删除。(首选项/键,然后将一组键映射到文件/删除尾随空格)如果您想清理所有新文件,但保持遗留代码不变,这可能很有用。
Another strategy is to activate visual display of whitespace, so at least you'll know when you're adding some trailing whitespace. As far as I know, there's no way to display only trailing whitespace though, but I'll be glad to be proved wrong.
另一种策略是激活空白的视觉显示,因此至少您会知道何时添加一些尾随空白。据我所知,虽然没有办法只显示尾随空格,但我很高兴被证明是错误的。
回答by Malvineous
In a pinch, for those editors that don't support removal of trailing whitespace at all (e.g. the XML editor), you can remove it from all lines by doing a find and replace, enabling regular expressions, then finding "[\t ]+$" and replacing it with "" (blank). There's probably a better regex to do that but it works for me without needing to install AnyEdit.
在紧要关头,对于那些根本不支持删除尾随空格的编辑器(例如 XML 编辑器),您可以通过执行查找和替换,启用正则表达式,然后找到 "[\t ] +$" 并将其替换为 ""(空白)。可能有更好的正则表达式可以做到这一点,但它对我有用,而无需安装 AnyEdit。
回答by Romain Linsolas
I would say AnyEdit too. It does not provide this specific functionalities. However, if you and your team use the AnyEdit features at each save actions, then when you open a file, it must not have any trailing whitespace.
我也会说 AnyEdit。它不提供此特定功能。但是,如果您和您的团队在每次保存操作时都使用 AnyEdit 功能,那么当您打开文件时,它不能有任何尾随空格。
So, if you modify this file, and if you add new trailing spaces, then during the save operation, AnyEdit will remove only these new spaces, as they are the only trailing spaces in this file.
因此,如果您修改此文件,并添加新的尾随空格,则在保存操作期间,AnyEdit 将仅删除这些新空格,因为它们是此文件中唯一的尾随空格。
If, for some reasons, you need to keep the trailing spaces on the lines that were notmodified by you, then I have no answer for you, and I am not sure this kind of feature exists in any Eclipse plugin...
如果出于某种原因,您需要在未由您修改的行上保留尾随空格,那么我无法回答您,而且我不确定任何 Eclipse 插件中是否存在这种功能...
回答by anatoly techtonik
It is impossibleto do it in Eclipse in generic way right now, but it can be changedgiven with basic Java knowledge and some free time to add basic support for this https://bugs.eclipse.org/bugs/show_bug.cgi?id=180349
现在不可能在 Eclipse 中以通用方式做到这一点,但是可以通过基本的 Java 知识和一些空闲时间来更改它以添加对此https://bugs.eclipse.org/bugs/show_bug.cgi 的基本支持? id=180349
The dependent issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=311173
回答by HeyMan
For php there is also an option:
对于 php,还有一个选项: