133

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.

Vy Do
  • 46,709
  • 59
  • 215
  • 313
klingt.net
  • 2,019
  • 3
  • 18
  • 19
  • 1
    You should accept the answer, assuming the setting is also there in IntelliJ 12. (And since it's in 11 and 13, I assume it's in 12.) – Chris Arena Feb 10 '14 at 21:28

5 Answers5

205

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

Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
Vic
  • 21,473
  • 11
  • 76
  • 97
  • 18
    There appears to be a bug in IDEA (including at least the v13.1.4 I am using now) that will, despite whatever you select in the preferences above, still save trailing spaces on the _line where the cursor is positioned_. For example, if you click at the end of any line and press `` a few times and then save (or switch to another app with autosave enabled), then that line will be saved with the trailing spaces intact. As soon as you move the cursor off the line and re-save, the spaces will go away. – Scott Dudley Oct 01 '14 at 18:34
  • 5
    It's 2016 and that bug is still there. – Cassio Pereira Apr 24 '16 at 17:59
  • Gotta watch out for this if you accidentally strip whitespace off some lines and want to put it back in there to make a cleaner diff. When you put the white-space back on the lines, intellij will strip it back off because putting it back "modifies" the line :) – David Mann May 08 '17 at 15:20
  • 3
    A caption **It is not a bug, it is a feature** doesn't actually convert a bug to a feature. It is super inconvenient to have a caret after the line end, but I want to remove trailing whitespaces from current line too (where the cursor is). For example, Sublime have this feature, and Sublime could remove trailing whitespaces _on demand_, and not only _on save_. – maxkoryukov May 14 '17 at 09:34
  • 5
    I'm not sure when it was implemented, but as of today, there is now a preference option that allows you to change this behavior: a checkbox labelled "Always keep trailing spaces on caret line", which is right under the "Strip trailing spaces on Save" option. – MaxML Aug 31 '17 at 15:10
  • 4
    I changed the settings in Intellij and it still doesnt work. – Ian Steffy Sep 18 '18 at 11:46
  • 1
    2019.1.3, this now actually respects the "Always keep training spaces on caret line = false" option, even when "Allow Placement of caret after end of line = false". – Mike Miller Jul 11 '19 at 20:16
20

In 2020.1 IntelliJ version:

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

Strip trailing spaces on Save option

zeljko_a
  • 3,725
  • 1
  • 22
  • 23
2

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

Despertaweb
  • 1,672
  • 21
  • 29
0

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.

mockinterface
  • 14,452
  • 5
  • 28
  • 49
0

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
  • I had the same situation. `trim_trailing_whitespace = false` should be changed to `trim_trailing_whitespace = true`. It was overriding the settings. – Zeth Sep 28 '22 at 14:01