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

Solution 1:

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.

Solution 2:

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

Solution 3:

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.

Solution 4:

The existing answers look old.

Try below path:

Notepad++ > Edit (menu) > Blank Operations > Trim Trailing Space

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