Automatic trimming trailing spaces

  • Lis

    Lis - 2010-09-14

    I like the notepad++, but permanently have some inconveniences.
    Because there's no way to automaticly remove trailing space from every line. Using a command from the "Edit" menu is a long way to do such a simple task.
    I would be appreciated if some day this feature is implemented.

  • Alexander Iljin

    Alexander Iljin - 2010-09-14

    What are you talking about? I've been using this feature since v4.8. You just have to use Shortcut Mapper to assign Ctrl+S to it.

  • cchris

    cchris - 2010-09-14

    It's called Edit -> Trim trailing spaces.


  • Bob Uncle

    Bob Uncle - 2010-09-14

    Are you serious?!

    I've been using this product for over 4 years (if not longer) and I have never noticed this feature before. Yikes! The amount deletes I could of saved is huge!

    Thanks Chris

    - Bob (who feels like an idiot now)

  • Lis

    Lis - 2010-09-16

    Thanks for your advice.

    In my version of Notepad++ there's a macro called "Trim trailing and save" with the short cut Alt+Shift+S. Obviously it has appeared during installation. It's relatively convinient.

    It's strange to me that trailing spaces irritate almost nobody. I hate these useless charactars. For ex. because when whant to delete a line I don't know how many times should I press Del before it's actualy deleted. I never know should I press Del, Backspace or select the line before deleting.

    In any case thanks.

  • Sune Marcher

    Sune Marcher - 2010-09-16

    In case you're not familiar with these standard shortcuts, try Ctrl+Del, Ctrl+Backspace - makes trailing spaces a lot less annoying :)

  • Lis

    Lis - 2010-09-16

    Thanks again.
    But it seems to me that the ability to trim trailing spaces ought to be a configurable feature of every text editor. Especially of an editor for programmers.

  • Alexander Iljin

    Alexander Iljin - 2010-09-17

    > But it seems to me that the ability to trim trailing spaces ought to be a configurable feature of every text editor. Especially of an editor for programmers.

    It IS configurable, especially BY programmers. Just think outside the box a little and move the "Ctrl+S" shortcut from "Save" to "Trim trailing and save".

  • Lis

    Lis - 2010-09-17

    Partially I may be satisfied because trailing spaces can be easily removed during saving.
    But I would like to have trailing spaces removed immediately after a line is typed or pasted from another source.
    There are a few cases when annoying spaces may appear. E.g.
    - when I started to type some spaces and changed my opinion to continue this line;
    - when I divide a line on two;
    - when I paste some text from "bad" source.
    And this ability is absent.

  • cchris

    cchris - 2010-09-17

    I think NppScripting or Python Script would allow you to schedule tasks (like trimming whitespace) every n seconds and before each save.

    Otherwise, you can change the Save shortcut to something and bind the Trim trailing space and save macro to Ctrl-S.


  • Anonymous - 2012-02-13

    Trim trailing and save should be the default, with an option in the dialog to prevent it in the rare case it is unwanted.

    "Oh, you can just do this four-click process to fix it… "

    While nice that the possibility exists, modifying a keybinding is not optimal, because:
       - there is no language or markup that utilizes trailing whitespace.
       - it creates noise in diffs and version control
       - it wastes bytes
       - it wastes time.

    Perhaps those with time to kill don't mind an extra thing to worry about but to those of us who have multiple copies of n++ installed on VMs and computer labs, we don't appreciate having to worry about an additional non-useful detail on every copy we use.  Is it enabled on this copy?  What a waste of time, to no appreciable benefit.

    In short, every professional editor provides this option directly, it's time for n++ to also.  Defaults matter.

  • Anonymous - 2012-02-13


    I just notice that if I use this macro with the cursor in the middle of a line it adds ten spaces at  the cursor! 


  • cchris

    cchris - 2012-02-18

    I just tried, and did not observe this undesired behaviour. Does it still happen if you rename your pugins directory to say "lugins"? I'm sure there is something interfering.


  • Anonymous - 2012-02-21

    Hi, now that you mention it, it only happens on one of my machines.  Nott sure why.  Newest n++, renaming plugins had no effect.

  • Ciprian Murariu

    Ciprian Murariu - 2013-07-21

    Just my 15 cents...
    I used UE before N++ (long time ago) and I remember UE automatically would have trimmed trailing slashes when saving a file (as an option in settings, of course). N++ would be great to have this implemented, saving us from forgetting to explicitly trim those extra buggy spaces introduced, accidentally by copy&paste God knows from which source.
    That is different than the hints above (I mean the Ctrl-S and Macro suggestions). Thanks.

    Last edit: Ciprian Murariu 2013-07-21
  • Predelnik

    Predelnik - 2013-07-23

    I've just added this code to

    def doRemoveTrailingWhiteSpaces(contents, lineNumber, totalLines):
      contents = contents.rstrip ('\n\r')
      stripped_contents = contents.rstrip ()
      if stripped_contents != contents:
        pos = editor.positionFromLine (lineNumber)
        editor.setTarget (pos + len (stripped_contents), pos + len (contents))
        editor.replaceTarget ("")
      return 1
    def removeTrailingWhiteSpaces(args):
      pos = editor.getCurrentPos ()
      line = editor.lineFromPosition (pos)
      indent = pos - editor.positionFromLine (line)
      # preserving current position
      new_line = editor.getLine (line)
      if indent >= len (new_line) - 1:
        new_pos = editor.getLineEndPosition (line)
        new_pos  = editor.positionFromLine (line) + indent
      editor.setSelectionStart (new_pos)
      editor.setSelectionEnd (new_pos)
    notepad.callback(removeTrailingWhiteSpaces, [NOTIFICATION.FILEBEFORESAVE])

    And in "plugins->python script->configuration" I've chosen initialisation "ATSTARTUP". It works fine, though sometimes N++ crashes and I can't seem to figure out why for now (though I noticed a lot of garbage I've been printing to console while copy & pasting it here, maybe that was the cause :D ). Hope it will be useful to somebody))

    EDIT: I optimized it a little so all the lines wouldn't be replaced on every save and also thus spellchecker underlines wouldn't be flickering.

    Last edit: Predelnik 2013-07-23
  • Loreia2

    Loreia2 - 2013-07-23

    Hi Predelnik,

    why do you need this python script? Is default ALT-SHIFT-S macro not sufficient for trimming extra white space?


    • Predelnik

      Predelnik - 2013-07-23

      No that's actually fine, I just didn't notice it, my bad :)

  • Peter Bruin

    Peter Bruin - 2014-04-16

    Is there any progress on this?

    This should really be configurable. All this workarounds are nice but if you expect a team to do this then you can't expect them to all do these things.

  • Christophe Meriaux

    You can use this macro :

    <Macro name="Trim Trailing and save" Ctrl="yes" Alt="no" Shift="no" Key="83">
    <Action type="2" message="0" wParam="42024" lParam="0" sParam="" />
    <Action type="2" message="0" wParam="41006" lParam="0" sParam="" />

    It trim trailing space and save the file. Shorcut Ctrl+S is used to replace the standard save fonction.

  • Al Le

    Al Le - 2014-04-16

    It would be even better if it could be configured depending on the file type. It's done very nicely in TextPad. But in NP++, the whole configuring would have to be revamped to achieve this.

  • Loreia2

    Loreia2 - 2014-04-21


    Notepad++ support for this is actually really good:

    CTRL+S <-- regular save, no trimming
    CRTL+SHIFT+S <-- save and trim whitespace

    Just use them as needed.



Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks