100

I don't want to delete the empty lines completely, but I want to remove the trailing tabs and whitespaces in them. Eclipse usually does it (through a preference setting) when we save the file.

For example, the file before saving ($ = end of line):

def shuffle(list):$
    import random $
    $
    random.shuffle(list)
    $
$

... and when I save this in the editor, Eclipse does the following:

def shuffle(list):$
    import random$
$
    random.shuffle(list)
$
$

How can I automatically trim trailing whitespace with Notepad++?

Stevoisiak
  • 13,555
  • 39
  • 101
  • 154
Srikanth
  • 4,799
  • 7
  • 31
  • 27
  • 1
    To trim whitespace from multiple files at once, do a Find & Replace for the regular expression `[ \t]+$`. ([Screenshot](https://i.stack.imgur.com/KI2WA.png)) ([Source](https://stackoverflow.com/a/9532388/3357935)) – Stevoisiak Sep 27 '17 at 20:13

10 Answers10

106

You should be able to do a regular expression find/replace in Notepad++ using a pattern like \s+$.

There are also a few options under menu Edit -> Blank Operations that may fit your needs.

Under the "Macro" menu there's an option for "Trim trailing and save". If you need to do a regular expression it may be possible to create a macro however I've never tried them.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
johanno
  • 1,174
  • 1
  • 8
  • 4
  • 13
    yes in my version of Notepad++, there's an `Edit -> Trim Trailing Space`, but can I trigger that automatically for every save? – Srikanth Jan 09 '14 at 18:36
  • 7
    There's an option for "Trim trailing and save" under the Macro menu – johanno Jan 09 '14 at 18:39
  • 30
    I have bound the Ctrl+S key to the `Macro -> Trim Trailing and save` option, using the `Settings -> Shortcut Mapper...` option. This is much better than having to remember to run the macro all the time. :) – Casey Kuball Jan 09 '14 at 22:54
  • 3
    Searching `\s+$` will match any empty line, regardless of whitespace. I recommend using `[ \t]+$` instead. (See [How do I remove trailing whitespace using a regular expression?](https://stackoverflow.com/a/9532388/3357935)) – Stevoisiak Sep 27 '17 at 19:48
88

Alt+Shift+S does what you want. In fact it also saves the file.

Update

As 10basetom noted, you can assign a different shortcut to this macro. You can control your shortcuts under Settings > Shortcut Mapper > [Macros].

enter image description here

Stevoisiak
  • 13,555
  • 39
  • 101
  • 154
kon psych
  • 981
  • 6
  • 6
  • 13
    To strip trailing spaces when you save: (1) Remove the default Save shortcut (Ctrl+S), then (2) assign the "Trim Trailing and save" macro to Ctrl+S. – thdoan Mar 03 '15 at 04:33
  • 2
    And to remove the default `Save` shortcut, double click on `Shortcut` field and then select `None` from the combo box. – Mitch Dec 06 '15 at 11:50
  • 2
    I found this page, updated my shortcuts as you suggested and used it happily ever after. Then recently I had to setup a new machine and had completely forgotten how I had done it before... Searched again and found this answer again! You should get +2 for helping me like this! – Stijn de Witt Jan 11 '16 at 20:59
  • Settings > Shortcut Mapper, then tab 'Macros' – bonob Feb 26 '16 at 09:22
  • 1
    This should be the selected answer for me ^^ – Nam G VU Feb 29 '16 at 04:17
  • In newer versions you find in the top Menu in `Run`->`Modify Shortcut/Delete command`->tab `Main menu`. Then clear the default `CTRL+S`, and then assign in tab `Macro` "Trim Trailing and save" to `CTRL+S` – João Pimentel Ferreira Aug 05 '17 at 15:51
7

The existing answers look old.

Try below path:

Notepad++ > Edit (menu) > Blank Operations > Trim Trailing Space
  • 1
    I recorded a macro to "Trim Trailing Spaces" AND "convert Tabs to Spaces", for consistency in version control. I then Saved the macro with keycode combination `Atl-T`. `Alt-T`, `Ctrl-S` is Quick combo to trim, convert and save. I purposely do this in two steps, as I do *not* want to trim markdown files, or change tabs/trailing when editing other maintainer's files that may default to a different standard than I use. `Notepad++ > Edit (menu) > Blank Operations > Trim Trailing Space` and `Notepad++ > Edit (menu) > Blank Operations > TAB to Space`. (N++ also has ... `Space to TAB` options.) – SherylHohman Jan 13 '19 at 23:21
6

Plugins > Plugin Manager > Show Plugin Manager
Under the Available tab, select EditorConfig and click [Install]

Add an .editorconfig file to the folder (name it .editorconfig. to avoid Windows error "You must type a filename" - the last dot will be removed)

# trims trailing whitespace for all files; filter like [*.{json,xml}]
[*]
trim_trailing_whitespace = true

EditorConfig can also specify encoding, indent and newline character(s), etc.

Webveloper
  • 161
  • 1
  • 1
  • 1
    Seems that the EditorConfig plugin for Notepad++ does not actually support the trim_trailing_whitespace option: https://github.com/editorconfig/editorconfig-notepad-plus-plus – jpa Sep 01 '15 at 08:32
  • 1
    Support for `trim_trailing_whitespace` was added on [2016-04-20](https://github.com/editorconfig/editorconfig-notepad-plus-plus/commit/4a0e81cf5d6c903310663adbdf05df7180397869). /cc @jpa – Stijn Dec 05 '17 at 21:37
  • I wish I could upvote this more, even if only for the `.editorconfig.` trick. To think that I had been resorting to creating dot-files via the Command Prompt like a caveman for all of these years. – jamesdlin Jan 18 '18 at 13:17
5

I changed the shortcuts to find a solution to this. I removed the save shortcut (shortcut mapper -> main menu -> save) and mapped Ctrl+S to the "Trim Trailing and Save" macro (shortcut mapper -> macros -> trim trailing and save). This way the macro replaces the save functionality and there's no need to remember the Alt+Shift+S shortcut.

Stevoisiak
  • 13,555
  • 39
  • 101
  • 154
Halcyon
  • 567
  • 2
  • 6
  • 10
2

Alt+Shift+S is the default shortcut for this. It's in the menu bar as Macro -> Trim Trailing and save. You can rebind this under Settings -> Shortcut Mapper -> [Macros].

Rebinding 'Trim Trailing and save' via Shortcut Mapper

Building on kon psych's answer, if you want to automatically trim whitespace any time you save, you can set this to replace the default Ctrl+S behavior. Just remember to change or remove the original save shortcut to prevent conflicts.

Disabling the default 'save' shortcut

Stevoisiak
  • 13,555
  • 39
  • 101
  • 154
  • +1 for 'Just remember to change or remove the original save shortcut to prevent conflicts.' – Wafeeq May 17 '18 at 12:03
1

These are the precise steps to redirect the standard "Save" shortcut Ctrl+S to do instead "Trim Trailing and Save"

  • Settings->Shortcut Mapper...
  • Main Menu tab, double click on "Save", change S to None
  • Macros tab, double click on "Trim Trailing Space and Save", change to Ctrl+S

The Macros shortcut can also be modified from Macro->Modify Shortcut/Delete Macro...

Antonio
  • 397
  • 1
  • 7
  • 23
1

Ctrl + F -> Switch to tab Replace ->

Find what: \t\r

Replace with: \r

Make sure Extended search mode is on, then replace all.

If you want to save as well, johanno has the correct solution. Macro -> Trim Trailing and Save works as specified.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
dr4g1116
  • 21
  • 7
1

In order to preserve the existing menu commands, map (previously unassigned) Ctrl+T to trim trailing spaces. Then, do Ctrl+T and Ctrl+S together.

This lets you make a small change to a file without confusing text comparison utilities and source control programs by changing (potentially) hundreds of lines.

Stevoisiak
  • 13,555
  • 39
  • 101
  • 154
al h
  • 11
  • 3
0

Another way -

  1. Edit > Blank Operations > Remove Unnecessary Blank and EOL
  2. Plugin > XML Tools > Pretty print (XML only - with line breaks)

'XML Tools’ is a plugin that we can install for notepad++