Removing empty lines in Notepad++

RegexNotepad++

Regex Problem Overview


How can I replace empty lines in Notepad++? I tried a find and replace with the empty lines in the find, and nothing in the replace, but it did not work; it probably needs regex.

Regex Solutions


Solution 1 - Regex

There is now a built-in way to do this as of version 6.5.2

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

Screenshot of removing empty lines

Solution 2 - Regex

You need something like a regular expression.

You have to be in Extended mode

If you want all the lines to end up on a single line use \r\n. If you want to simply remove empty lines, use \n\r as @Link originally suggested.

Replace either expression with nothing.

Solution 3 - Regex

There is a plugin that adds a menu entitled TextFX. This menu, which houses a dizzying array of quick text editing options, gives a person the ability to make quick coding changes. In this menu, you can find selections such as Drop Quotes, Delete Blank Lines as well as Unwrap and Rewrap Text

Do the following:

TextFX > TextFX Edit > Delete Blank Lines
TextFX > TextFX Edit > Delete Surplus Blank Lines

Solution 4 - Regex

  1. notepad++
  2. Ctrl-H
  3. Select Regular Expression
  4. Enter ^[ \t]*$\r?\n into find what, leave replace empty. This will match all lines starting with white space and ending with carriage return (in this case a windows crlf)
  5. Click the Find Next button to see for yourself how it matches only empty lines.

Solution 5 - Regex

  1. Press ctrl + h (Shortcut for replace).
  2. In the Find what zone, type ^\R ( for exact empty lines) or ^\h*\R ( for empty lines with blanks, only).
  3. Leave the Replace with zone empty.
  4. Check the Wrap around option.
  5. Select the Regular expression search mode.
  6. Click on the Replace All button.

enter image description here

Solution 6 - Regex

You can follow the technique as shown in the following screenshot:

  • Find what: ^\r\n
  • Replace with: keep this empty
  • Search Mode: Regular expression
  • Wrap around: selected

enter image description here

NOTE: for *nix files just find by \n

Solution 7 - Regex

This worked for me:

  1. Press ctrl + h (Shortcut for replace)
  2. Write one of the following regex in find what box. [\n\r]+$ or ^[\n\r]+
  3. Leave Replace with box blank
  4. In Search Mode, select Regex
  5. Click on Replace All

    Done!

Solution 8 - Regex

In notepad++ press CTRL+H , in search mode click on the "Extended (\n, \r, \t ...)" radio button then type in the "Find what" box: \r\n (short for CR LF) and leave the "Replace with" box empty..

Finally hit replace all

Solution 9 - Regex

Well I'm not sure about the regex or your situation..

How about CTRL+A, Select the TextFX menu -> TextFX Edit -> Delete Blank Lines and viola all blank line gone.

A side note - if the line is blank i.e. does not contain spaces, this will work

Solution 10 - Regex

  1. Ctrl + H ( Or Search 🠆 Replace..) to open Replace window.

  2. Select 'Search Mode' 'Regular expression'

  3. In 'Find What' type ^(\s*)(.*)(\s*)$ & in 'Replace With' type \2

  • ^ - Matches start of line character
  • (\s*) - Matches empty space characters
  • (.*) - Matches any characters
  • (\s*) - Matches empty spaces characters
  • $ - Matches end of line character
  • \2 - Denotes the matching contend of the 2nd bracket

enter image description here Refer https://www.rexegg.com/regex-quickstart.html for more on regex.

Solution 11 - Regex

You can search for the following regex: ^(?:[\t ]*(?:\r?\n|\r))+ and replace it with empty field

Solution 12 - Regex

Ctrl+H.

find - \r\r replace with - \r.

Solution 13 - Regex

This obviously does not work if the blank lines contain tabs or blanks. Many web pages (e.g. http://www.guardian.co.uk/) contain these white lines, as a result of a faulty HTML editor.

Remove white space using regular expression as follows:

> change pattern: [\t ]+$ > into nothing.

where [\t ] matches either tab or space. '+' matches one or more occurrences, and '$' marks the end of line.

Then use notepad++/textFX to remove single or extra empty lines. Be sure that these blank lines are not significant in the given context.

Solution 14 - Regex

  1. Edit >> Blank Operations >> Trim Leading and Trailing Spaces (to remove black tabs and spaces in empty lines)
  2. Ctrl + H to get replace window and replace pattern: ^\r\n with nothing (select regular expression)

Note: step 1 will remove your code intendation done via tabs and blank spaces

Solution 15 - Regex

enter image description here

Sometimes \n\r etc not work, here to figure it out, what your actually regular expression should be.

Advantage of this trick: If you want to replace in multiple file at once, you must need this method. Above will not work...

Solution 16 - Regex

CTRL+A, Select the TextFX menu -> TextFX Edit -> Delete Blank Lines as suggested above works.

But if lines contains some space, then move the cursor to that line and do a CTRL + H. The "Find what:" sec will show the blank space and in the "Replace with" section, leave it blank. Now all the spaces are removed and now try CTRL+A, Select the TextFX menu -> TextFX Edit -> Delete Blank Lines

Solution 17 - Regex

/n/r assumes a specific type of line break. To target any blank line you could also use:

^$

This says - any line that begins and then ends with nothing between. This is more of a catch-all. Replace with the same empty string.

Solution 18 - Regex

I did not see the combined one as answer, so search for ^\s+$ and replace by {nothing}

^\s+$ means
  ^ start of line
  \s+ Matches minimum one whitespace character (spaces, tabs, line breaks)
  $ until end of line

Solution 19 - Regex

This pattern is tested in Notepad++ v8.1.1

It replaces all spaces/tabs/blank lines before and after each row of text.

It shouldn't mess with anything in the middle of the text.

Find: ^(\s|\t)+|(\s|\t)+$

Replace: leave this blank


Before:
_____________________________________
\tWORD\r\n
\r\n
\tWORD\s\tWORD\s\t\r\n
\r\n
\r\n
WORD\s\s\tWORD\t\sWORD\s\r\n 
\t\r\n
\s\s\s\r\n
WORD\s\sWORD\s\s\t\r\n

____________________________________


After:
_____________________________________
WORD\r\n
WORD\s\tWORD\r\n
WORD\s\s\tWORD\t\sWORD\r\n
WORD\s\sWORD
_____________________________________

Solution 20 - Regex

A few of the above expressions and extended expressions did not work for me, but the regular expression "$\n$" did.

Solution 21 - Regex

An easy alternative for removing white space from empty lines:

  1. TextFX>TextFX Edit> Trim Trailing Spaces

This will remove all trailing spaces, including trailing spaces in blank lines. Make sure, no trailing spaces are significant.

Solution 22 - Regex

this work for me:

SEARCH:^\r  
REPLACE:            (empty)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnomoreflashView Question on Stackoverflow
Solution 1 - RegexHalfwarrView Answer on Stackoverflow
Solution 2 - RegexBradView Answer on Stackoverflow
Solution 3 - Regexuser238469View Answer on Stackoverflow
Solution 4 - RegexSleepyBoBosView Answer on Stackoverflow
Solution 5 - RegexAlex.K.View Answer on Stackoverflow
Solution 6 - RegexSaikatView Answer on Stackoverflow
Solution 7 - RegexKamal NayanView Answer on Stackoverflow
Solution 8 - RegexBassemView Answer on Stackoverflow
Solution 9 - RegexAhmadView Answer on Stackoverflow
Solution 10 - RegexPrabodaView Answer on Stackoverflow
Solution 11 - RegexYa BashaView Answer on Stackoverflow
Solution 12 - Regexmahesh adepuView Answer on Stackoverflow
Solution 13 - RegexPoissonView Answer on Stackoverflow
Solution 14 - RegexAdeebView Answer on Stackoverflow
Solution 15 - RegexWasim A.View Answer on Stackoverflow
Solution 16 - Regexuser2189974View Answer on Stackoverflow
Solution 17 - RegexAshBradView Answer on Stackoverflow
Solution 18 - Regexuser2056154View Answer on Stackoverflow
Solution 19 - Regexslyfox1186View Answer on Stackoverflow
Solution 20 - RegexChrisView Answer on Stackoverflow
Solution 21 - RegexpoissonView Answer on Stackoverflow
Solution 22 - RegexRafael MarquesView Answer on Stackoverflow