MavrosGatos
MavrosGatos

Reputation: 529

Notepad++ replace lines containing only spaces and/or tabs

How can I replace all the lines in my code that contain only spaces and/or tabs with empty lines?

Example:

for(var l=0,leng=one.codes.length;l<leng;l++){
******
    var oneCode = one.codes[l];
    var temp = new Object();
******
    temp.code = oneCode.c;
******
    if(two >= 1000){
        var alex = "";
        var bob = 0;
    }
******
}

The * symbols are the spaces and tabs. I want them removed without changing the rest of the code format. I want this lines to remain blank but without containing any characters.

Upvotes: 3

Views: 3701

Answers (3)

Tryph
Tryph

Reputation: 6209

  1. Ctrl + H to open the Replace tab
  2. type ^\s+$ in the Find what text field
  3. let the Replace with text field empty
  4. select Regular expression in the Search Mode frame
  5. Click the Replace All button (you may want to click find button few times before to check that the regex is correct)

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627190

You may use

^\h+$

where:

  • ^ - start of the line
  • \h+ - one or more horizontal whitespace symbols
  • $ - end of line

Note: to also remove the line breaks, add a \R* - zero or more line breaks - at the end of the pattern.

enter image description here

                     VVVV

enter image description here

Upvotes: 4

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522301

You can use the following find and replace:

Find:

(?![^\s]).(\r?\n)

Replace:

$1

This regex uses a negative lookahead, which will assert that at each point in the line the character which follows is not anything but whitespace (including space and tab).

The \r?\n end of the regex will match either \r\n, which is the Windows line ending, or just \n, which is the Unix line ending. This is captured in $1 and then used as the replacement, since you want to keep the line, while removing all whitespace.

enter image description here

Upvotes: 2

Related Questions