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

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

Remove trailing whitespace on save in IntelliJ IDEA 12

gitintellij-ideareformat

提问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

设置 → 编辑器 → 常规 → 在保存时去除尾随空格

configuration dialogue

配置对话

回答by Despertaweb

Go to ==> PREFERENCES | GENERAL | OTHER |

转到 ==> 首选项 | 一般 | 其他 |

Just as shown in the picture:

正如图片所示:

  1. Srip trailings spaces on Save: ALL
  2. UncheckAllways keep....
  1. 保存时删除尾随空格:全部
  2. 取消选中始终保持....

It'll remove trailing spaces when save, not before

它会在保存时删除尾随空格,而不是之前

enter image description here

在此处输入图片说明

回答by zeljko_a

In 2020.1 IntelliJ version:

在 2020.1 IntelliJ 版本中:

File -> Settings -> Editor -> General -> then scroll down to 'Save Files'

文件 -> 设置 -> 编辑器 -> 常规 -> 然后向下滚动到“保存文件”

Strip trailing spaces on Save option

删除保存选项上的尾随空格

回答by mockinterface

Add an external tool. As the Program pass /usr/bin/sed(may be different on your box, run which sedto 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 非常相似。接下来,您可以为新的外部工具条目添加键盘快捷键,但我不确定是否可以在保存时运行任何内容。