• Remove Trailing Spaces

    by  • October 27, 2012 • Developers, Users • 11 Comments

    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…

    About

    Dominik is a PhD student at the Control Theory and Robotics Lab, TU Darmstadt, as part of the Research Training Group GKMM (GRK1362). My research focuses on state estimation in distributed systems. As hobby, I contribute to the KDE project and work on the Kate application and editor component.

    http://www.kate-editor.org

    11 Responses to Remove Trailing Spaces

    1. October 27, 2012 at 17:16

      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. October 27, 2012 at 17:47

      I will definitely use the “Modified Lines” mode. Great work!

    3. Ian Monroe
      October 28, 2012 at 05:32

      This is awesome thanks. Modified Lines is for sure what people who work with others should use.

    4. anonymity is great
      October 29, 2012 at 13:34

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

      • October 29, 2012 at 21:05

        Changed to On Modified Lines and In Entire Document, thanks for the suggestion.

    5. John Layt
      October 29, 2012 at 14:08

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

    6. October 29, 2012 at 16:14

      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.

    7. SeanT
      February 28, 2013 at 17:02

      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.

      • Bob
        June 1, 2013 at 17:14

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

    8. Matthew Woehlke
      March 21, 2013 at 23:46

      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’?)

      • March 25, 2013 at 12:02

        Can you send a mail to kwrite-devel@kde.org about this? I’m not 100% sure, but messing with the text line length in wrap-line might introduce bugs and that’s why Christoph removed that. I’m note sure 100%, though…