git 在 IntelliJ IDEA 12 中保存时删除尾随空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20679842/
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
Remove trailing whitespace on save in IntelliJ IDEA 12
提问by klingt.net
Is it possible to remove trailing whitespace automatically on save in IntelliJ IDEA? I know there are some workarounds, for example, using git to trim the whitespace on commit. Maybe this question is a duplicate of this one, but i hope this can be done without setting up keyboard shortcuts and macros.
是否可以在 IntelliJ IDEA 中保存时自动删除尾随空格?我知道有一些解决方法,例如,使用 git 在提交时修剪空格。也许这个问题是一个重复的这一个,但我希望这可以在不设置键盘快捷键和宏命令来完成。
回答by Vic
Don't know about 12, but there's the following setting in 13:
不知道12,但13中有以下设置:
Settings → Editor → Strip trailing spaces on Save
设置 → 编辑器 → 在保存时去除尾随空格
As of IntelliJ 2017.2 it's under
从 IntelliJ 2017.2 开始,它在
Settings → Editor → General → Strip trailing spaces on Save
设置 → 编辑器 → 常规 → 在保存时去除尾随空格
回答by Despertaweb
回答by zeljko_a
回答by mockinterface
Add an external tool. As the Program pass /usr/bin/sed
(may be different on your box, run which sed
to locate) and insert the -i 's/[[:space:]]\+$//' $FilePath$
in the Parameters. Overall the command that you want IntelliJ to run is,
添加外部工具。随着程序通过/usr/bin/sed
(可能与您的盒子不同,运行which sed
以定位)并-i 's/[[:space:]]\+$//' $FilePath$
在参数中插入。总的来说,您希望 IntelliJ 运行的命令是,
/usr/bin/sed -i 's/[[:space:]]\+$//' <your current file>
This sed will remove the trailing whitespace, and overall the effect will be very similar to git. Next you can add a keyboard shortcut for your new external tool entry, but I am not sure whether it is possible to run anything on save.
这个 sed 会去掉尾随的空格,整体效果和 git 非常相似。接下来,您可以为新的外部工具条目添加键盘快捷键,但我不确定是否可以在保存时运行任何内容。