Remove trailing whitespace on save in IntelliJ IDEA 12

GitIntellij IdeaReformat

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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

configuration dialogue

Solution 2 - Git

In 2020.1 IntelliJ version:

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

Strip trailing spaces on Save option

Solution 3 - Git

Go to ==> PREFERENCES | GENERAL | OTHER |

Just as shown in the picture:

  1. Srip trailings spaces on Save: ALL
  2. Uncheck Allways keep....

It'll remove trailing spaces when save, not before

enter image description here

Solution 4 - Git

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.

Solution 5 - Git

What worked for me was

Intelij

  1. Settings
  2. General
  3. On Save
  4. Unchecking remove trailing spaces

Workspace

  1. Find .editorconfig
  2. Set "trim_trailing_whitespace" setting.
[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = false

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionklingt.netView Question on Stackoverflow
Solution 1 - GitVicView Answer on Stackoverflow
Solution 2 - Gitzeljko_aView Answer on Stackoverflow
Solution 3 - GitDespertawebView Answer on Stackoverflow
Solution 4 - GitmockinterfaceView Answer on Stackoverflow
Solution 5 - GitPavel KunitskyView Answer on Stackoverflow