Mike
Mike

Reputation: 2391

regex in Notepad++ to remove blank lines

I have multiple html files and some of them have some blank lines, I need a regex to remove all blank lines and leave only one blank line.. So it removes anything more than one blank line, and leave those that are just one or none (none like in having text in them).

I need it also to consider lines that are not totally blank, as some lines could have spaces or tabs (characters that doesn't show), so I need it to consider these lines with the regex to be removed as long as it is more than one line..

Upvotes: 5

Views: 19667

Answers (8)

NelemaniA
NelemaniA

Reputation: 11

In notepad++ v8.4.7 there is the option:

Edit > Line Operations > Remove Empty Lines (Containing Blank characters)

or

Edit > Line Operations > Remove Empty Lines

So there is no need to use a regular expressions for this. But this only works for one file at a time.

Upvotes: 1

gdenuf
gdenuf

Reputation: 890

I looked for ^\r\n and click "Replace All" with nothing (empty) in "Replace with" textbox.

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336088

Search for

^([ \t]*)\r?\n\s+$

and replace with

\1

Explanation:

^         # Start of line
([ \t]*)  # Match any number of spaces or tabs, capture them in group 1
\r?\n     # Match one linebreak
\s+       # Match any following whitespace
$         # until the last possible end of line.

\1 will then contain the first line of whitespace characters, so when you use that as the replacement string, only the first line of whitespace will be preserved (excluding the linebreak at the end).

Upvotes: 9

Wasim A.
Wasim A.

Reputation: 9890

enter image description here

You can find it yourself what you need to replace with \n\n OR \n\r\n or \r\n\r\n etc ... now you can even modify your regular expression ^([ \t]*)\r?\n\s+$ according to your need.

Upvotes: 2

Campfire
Campfire

Reputation: 894

I tested any of the above suggestions, always was either too less or to much deleted. So that either you got no blank line where at least one was beforehand or deleted not enough (whitespaces was left, etc.). Unfortunately I cannot write comments yet. Tested both with 6.1.5 and updated to 6.2 and tested again. depending on how mayn files there are, I would suggest use

Edit->Blank Operations->Trim trailing whitespace

Followed by Ctrl+A and

TextFX -> TextFX Edit -> Delete surplus blank lines

A Macro I tried to record didn't work. Theres even a macro for just remove trailing whitespace (Alt+Shift+S, see Settings | Shortcut Mapper... | Macros). There's a

Edit->Blank Operations->Remove unnecessary EOL and whitespace

but that deletes every EOL and puts everything in a single line.

Upvotes: 1

Ivan
Ivan

Reputation: 171

This worked for me on notepad++ v6.5.1. UNICODE windows 7

Search for: ^[ \t]*\r\n

Replace with: nothing, leave blank

Search mode: Regular expression.

Upvotes: 9

dlock
dlock

Reputation: 9577

This will replace the successive blank lines containing white spaces (or not) and replace it with one new line.

Search for (\s*\r?\n){3,}

replace with \r\n

Upvotes: 2

escitalopram
escitalopram

Reputation: 3856

search for (\r?\n(\t| )*){3,}, replace by \r\n\r\n, check "Regular expression" and ". matches newline".

Tested with Notepad++ 6.2

Upvotes: 2

Related Questions