Remove trailing whitespace on save in IntelliJ IDE

2019-03-17 10:49发布

问题:

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.

回答1:

Don't know about 12, but there's the following setting in 13:

Settings → Editor → Strip trailing spaces on Save

As of IntelliJ 2017.2 it's under

Settings → Editor → General → Strip trailing spaces on Save



回答2:

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 -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.