Nasser
Nasser

Reputation: 13131

How to stop intellij-idea from removing white spaces at end of text lines in my file?

I am using 2018.1.5 community edition of intellij editor to edit a plain text file.

I am not using a project. I start it, on Linux as follows:

  idea.sh  my_file.mpl

where my_file.mpl is plain text file.

And this works well, except for one big problem.

I need to have an empty space at end of some lines. i.e. after the line character on some line in the file, I insert some white. I see the space is there, by doing View->Active editor->Show White spaces. I can see the small tiny dotts, showing there are white space character at end of line.

But as soon as I save the file, these white spaces are removed from end of line.

This causes a problem for me (for other reason, when this file is read by another app).

Is there an option to tell it intellij NOT to remove white spaces after the last character on the line?

Upvotes: 49

Views: 33847

Answers (6)

user2051552
user2051552

Reputation: 3144

Using IntelliJ IDEA 20232.2.5 Try this first approach - then if it doesn't work, try the second approach.

1. In Settings, go to Editor->General, and then scroll down to Save Files section. Uncheck Remove trailing spaces

Screenshot of Remove Trailing Spaces

2. If that doesn't work, there may be an editor config overriding it. If you don't need it, turn it off

In Settings, go to Editor->Code Style. Uncheck Enable EditorConfig support

Screenshot of Enable EditorConfig support location

Upvotes: 4

jonyB
jonyB

Reputation: 11985

So here's what worked for me. I wanted intellij to keep trimming whitespaces but to keep the whitespace on blank lines.

First like @Michael-Veksler suggested, I changed intellij's default removal on save.

But it kept trimming the trailing whitespace, that was because in my .editorconfig file I had a trim_trailing_whitespace config turned on. So now I've set trim_trailing_whitespace = false.

Lastly, because I still wanted the editor to trim the trailing whitespaces, just not on blank lines I've added to my .eslintrc.js file the following setting:

module.exports = {
  ...
  rules: {
    ...
    'no-trailing-spaces': [2, { "skipBlankLines": true }]
  }
}

And now lint-fix on save just takes care of it

Upvotes: 1

Viraths
Viraths

Reputation: 950

I had my .editorconfig and I have used trim_trailing_whitespace=true. So that was the issue. This is what I did to fix the issue.

[*.md]
trim_trailing_whitespace=false

Upvotes: 7

Brian O Carroll
Brian O Carroll

Reputation: 500

If changing the settings in "Editor > General > Save Files" doesn't work, the below might be useful...

There is a discussion here about a possible bug (or at least confusing scenario) where you have set "Strip trailing spaces on Save for"=None and you still get stripping of trailing spaces, as I did. The comment by Oksana Chumak worked for me - namely, I unticked "Enable EditorConfig Support" in Settings > Editor > Code Style. This seems to allow a config file ".editorconfig" to be used which overrides some settings - also see Andriy Bazanov's answer, quoted below...

NOTE: If you have .editorconfig file in your project, such option can also be controlled via that file. This file can provide more granular control over what files are affected by those settings.

Settings from .editorconfig file will OVERWRITE IDE settings (the whole nature of such files), so if you have such files in your project and such instruction there, you will have to either disable the EditorConfig for this project or disable plugin completely (if you do not care about it at all).

Upvotes: 4

Michael Veksler
Michael Veksler

Reputation: 8475

Newer PyCharm (2020 and beyond) have slightly different answer than Code-Apprentice's.

Go to File->Settings->Editor->General, and then scroll down to Save Files section. The drop down Strip trailing spaces on Save for: is there. You can select None if you don't want any stripping in no circumstances.

Here is an image showing the setting: Image describing the location of the drop-down

Upvotes: 25

Code-Apprentice
Code-Apprentice

Reputation: 83527

Go to File->Settings->Editor->General and under Other, set the drop down next to Strip trailing spaces on Save to whatever you wish. For future reference, you can press Ctrl-Shift-A and type a search term to find any menu command or setting very quickly. In this case, "trailing spaces" or "strip trailing spaces" works really well.

Upvotes: 54

Related Questions