How can I remove lines that begin with spaces in Notepad++?

  • Ctrl+H
  • Find what: ^\h+.*$\R?
  • Replace with: LEAVE EMPTY
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

^           # beginning of line
  \h+       # 1 or more horizontal spaces
  .*        # 0 or more any character but newline
$           # end of line
  \R?       # optional linebreak

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here


It's possible with a pure regex solution (see @Toto's answer) but perhaps the following solution, using bookmarks, is easier to grasp:

  1. Open the Find dialog

  2. Go to the Mark tab, search for ^ (that's 2 characters: a caret indicating 'the beginning of the line' and a space) and enable the 'Bookmark line' option. I assume your Search Mode is already set to Regular expression. Hit the 'Mark All' button.

    enter image description here

  3. Delete all bookmarked lines (Search → Bookmarks → Remove bookmarked lines)


You can do this:

  • Hit Ctrl+H for find and replace.

  • Check mark regular expression.

  • Use the Regex ^\s+.* in the Find box.

  • Keep Replace box blank.

  • Click Replace All.