Remove Trailing Spaces

Up to KDE 4.9, Kate Part had support to remove trailing spaces in two ways:

  1. Remove trailing spaces while editing
  2. Remove trailing spaces on save

The reasoning behind removing trailing spaces while editing is that when working on a document, we want to keep our own changes clean of trailing spaces. This way, we can for instance provide patches that are not cluttered with whitespace changes, and we just change lines that we really want to change.

The implementation of this feature unfortunately had quite some regressions that we were able to “fix” over time. For instance, you do not want to remove trailing spaces if the cursor is currently in the trailing spaces area. This alone means we have to kind of remember that we touched this line, and then remove it later. This was always hacky, and in fact, there are still corner cases that did not work.

For KDE 4.10, the both options were merged into just one option Remove trailing spaces with three possible values:

So we only support removing trailing spaces on save from KDE 4.10 on. The implementation is now very clean and based on the line modification system available since KDE 4.8: Thanks to this system we know exactly which lines in the document were changed. So if you choose “Modified Lines” in the configuration, trailing spaces of these modified lines are removed, and other lines remain untouched. If you choose “Entire Document”, then all trailing spaces in the document will be removed. And, needless to say, “Never” implies that trailing spaces are never removed.

For compatibility, the old mode-lines “remove-trailing-space” and “replace-trailing-spaces-save” are still supported, but you’ll get a kWarning() on the console. All these changes are also documented in the Kate handbook (once KDE 4.10 is released). From KDE 4.10 on, you should switch to the modelines

- remove-trailing-spaces none;
– remove-trailing-spaces modified;
– remove-trailing-spaces all;

Hope you like it…

15 thoughts on “Remove Trailing Spaces”

  1. Great idea! I never considered what the “remove trailing spaces while editing” was for, but it meets exactly what I like to do: no trailing space on new/changed code, but don’t change spaces already there so the diff is still useful.

  2. Nice change. The new options make much more sense than the old ones. I always wondered which trailing spaces were actually removed when the option “Remove trailing spaces while editing” was selected (because of this uncertainty and laziness, I never tried this option), this option tells only *when* the spaces are removed, not *which* spaces.

    Maybe, for more clarity, you can rename the two latter options “On Modified Lines” and “In Entire Document”.

  3. Brilliant! Exactly how I want it to behave, I had to turn it off because it was messing up git diffs. Thank you!

  4. Great! Looks like what QtCreator does, which usually works very well. In latest versions they changed it to clean up whitespace automatically in modified lines — not just trailing spaces, but converts tabs to spaces or vice versa according to your settings.

  5. Well, I am one that I **DISLIKE** this decision to the point I am considering rolling back to KDE 4.9 just to have again the options of removing trailing spaces while pasting into Kate (never tried notepad++ in wine but I might now).

    I have always used Kate to clean up text I copied from other sources when the final destination for said text was, for example, and Office document (so no point in saving the text). Now it no longer works with latest version of Kate. Really disappointed !!

    The right thing that should have been done was to implement at least a tool inside Kate that is capable of removing trailing spaces from the document. And it SHOULD NOT require you to save the document to accomplish that.

    1. Agreed. Kate was good for cleaning up text copied from Konsole without saving. I too miss that feature.

  6. I like the new option for on-save cleanup to only apply to modified lines; no reason now to use ‘never’ in my book :-) except in corner cases where you actually need to have trailing whitespace for whatever reason.

    However, I don’t like that kate now leaves trailing space when e.g. I break an existing line into two lines, or on the previous line(s) after hitting ‘enter’ a few times to add blank lines.

    Would it be possible to get *just* that back? (IOW, remove trailing space from the previous line when inserting a new line with ‘enter’?)

  7. “For instance, you do not want to remove trailing spaces if the cursor is currently in the trailing spaces area.”

    Why not? This “feature” is very annoying, and results in whitespace still appearing in diffs when it should not, all because of where my cursor happened to be sitting at the time I clicked ‘save’.

    If someone selects that they want whitespace removed from the “Entire Document”, surely this means they actually want whitespace removed from the *entire* document…

    1. Well, it certainly would annoy me. I save constantly while editing, and I don’t want that line I’m currently working on to get “cleaned“ while I’m on it. For instance, if I’m in a deep intendation and about to start a line, I don’t want saving to remove that.

      1. What if your “deep indentation” was only removed in the saved file, but not in the active document?

        Another way of looking at it is to remember trailing spaces on the current line, remove them, save the document, then put them back, ensuring the cursor is in the same place it was.

        The implementation might take some other form but that is the result…would that work?

        Failing all of that, perhaps we could at least have an option in the settings and that could keep everyone happy…

        In the meantime I’ve patched it myself and will continue to build kate from source with my patch in place until this is resolved.

Leave a Reply