113

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
  • 16,075
Srikanth
  • 5,219

12 Answers12

115

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.

johanno
  • 1,264
98

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
  • 16,075
kon psych
  • 1,081
10

The existing answers look old.

Try below path:

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

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.

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
  • 16,075
Halcyon
  • 577
4

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
  • 16,075
2

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
  • 427
1

I found a menu item in version 8.7.5 Edit | Blank Operations | Trim Trailing Space.

It deleted the trailing space on every offending line in my file. There is no keystroke shortcut associated with it by default. You may modify and add your own shortcut.

help-info.de
  • 2,159
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.

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
  • 16,075
al h
  • 11
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++

0

I only wanted to trim non-empty lines. Building off an earlier comment, I searched for (\S)[ \t]+$ and replaced with $1. This leaves blank auto-indented lines unchanged.

Andy
  • 1