445

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.

Ry-
  • 218,210
  • 55
  • 464
  • 476
nomoreflash
  • 4,579
  • 3
  • 16
  • 11

22 Answers22

638

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

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Halfwarr
  • 7,853
  • 6
  • 33
  • 51
  • 1
    Unfortunately this doesn't consider your selection, it will delete all the empty lines in the file even if you have selected only some of them. – Fabio says Reinstate Monica Oct 11 '16 at 11:53
  • 2
    If this is a one-time operation, you can extract the selected lines to the new notepad++ tab and perform that operation there. Copy-paste procedure for 5seconds. Any other workaround that cannot be accomplished in that time is a waste in my opinion. But if you are doing that every day 100 times... – creed Oct 16 '18 at 10:17
  • I was using this (even assigned my shortcut), but it's a lot slower than option above, so use smartly... – Betlista Jun 09 '19 at 20:43
437

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.

Brad
  • 15,361
  • 6
  • 36
  • 57
  • 23
    Should first select 'extended` in search mode. – q0987 Apr 17 '12 at 15:47
  • It should be noted that replacing all on `\r\n\r\n` can cause 2 of your lines to be joined together. `23` Text on line 23!`\r\n` `24 \r\n` `25\r\n` `26`Line 26 text.`r\n` can be compressed to `23` Text on line 23!Line 26 text. – Robert Kaucher Jul 13 '12 at 14:33
  • 15
    To get your lines not to be joined together you have to search for `\r\n\r\n` and replace with `\r\n`. – Robert Kaucher Jul 13 '12 at 14:48
  • 1
    See @BoltClock's comment below. Also, if your file is saved with Unix line endings, you can convert it to Windows line endings in Edit > EOL Conversion > Windows Format before doing the find and replace. – Big McLargeHuge Aug 16 '12 at 15:51
  • 11
    If you wish to remove lines with only spaces and tabs or nothing try using regex replace to match `\r\n\W*\r\n` and replace with `\r\n` – Myster Apr 14 '14 at 02:25
  • 1
    @Myster First thing to say I barely know regex, so apologize if I'm wrong. I think your suggestion wouldn't do the trick with many empty lines in a row, have you tested it and does it really works? – ST3 Aug 17 '15 at 12:31
  • 4
    @ST3 hmm what was I thinking, This one is much better: `(\r\n|\r|\n)(\s*(\r\n|\r|\n))+` and replace with `\r\n` see it in action here: https://regex101.com/r/qD9dB1/1 – Myster Aug 17 '15 at 23:58
  • What on earth are you talking about when you say \n\r you do realise you have that backwards don't you? – barlop Mar 18 '18 at 04:48
  • @barlop, `\n\r` is for removing completely empty lines. for example `line 1\r\n\r\nline 3`: removing \n\r will leave you with `line 1\r\nline 3` – Brad Apr 15 '19 at 22:53
123

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
kolufild
  • 712
  • 1
  • 9
  • 20
  • 7
    Not sure as of when, but it would seem that this plugin is installed by default now. Slightly off-topic, but one of the other incredibly useful features, under `TextFX > TextFX Tools`, is line sorting, optionally removing duplicates. Very handy way of doing a Unix `sort -u` on a Windows box without cygwin or unxutils or similar. – Cam Jackson Oct 06 '11 at 01:00
  • 3
    [Here's a link to the TextFx Plugin.](http://sourceforge.net/projects/npp-plugins/files/TextFX/) I found it very useful. Use: `Settings > Import > plugin...` and find the dll file to install it. It becomes a menu item by `Plugins` – Nick Pickering Nov 02 '12 at 19:41
  • 1
    will this work on all open documents, or one document at a time? – derelict May 31 '16 at 21:20
102
  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.
Sameer
  • 101
  • 2
  • 11
SleepyBoBos
  • 1,411
  • 2
  • 11
  • 16
  • 1
    I had to use this method instead of some of the other methods to get it to easily work in a macro. Thank you. – Mafu Josh Oct 09 '14 at 15:32
  • 7
    even shorter and also lesser replacements needed because of multiple empty lines will be replaced at once: ^\s*$\r?\n – njank Dec 16 '17 at 20:00
  • In notepad++ i used `(^[ \t]*$\s)\s*` in the find box and left replace empty. Works great. Thanks! – slyfox1186 Jun 20 '20 at 22:52
  • why does `^\s*$` work for multiple lines? shouldn't ^$ "restrict" it to one line? (@njank or anyone else) – Albin Dec 01 '20 at 22:56
  • Thanks for this regex expression, it worked well, but it took a while to complete execution. – abunickabhi Feb 04 '21 at 08:55
  • 1
    @albin, this pattern is matching a (possibly blank) line followed by a (possible single carriage return and) newline. It's the combination of "more than one blank line" that gives it so much power. I used the suggestion from njank and specified Replace with \n -- this replaced multiple blank lines with a single blank line. – Lyle S. Jun 16 '23 at 23:57
41
  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

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
  • 2
    This worked for me without any issues or additional settings to change. Thanks! – Bonez024 Jul 30 '19 at 13:27
  • Except that it's shorter, what exactly is the difference to [SleepyBoBos's answer](https://stackoverflow.com/a/14064039/2089396)? – Albin Nov 30 '20 at 14:18
  • @Albin, different regex: SleepyBoBos: ^[ \t]*$\r?\n This will match all lines starting with white space and ending with carriage return (in this case a windows crlf) My version: ^\R ( for exact empty lines) or ^\h*\R ( for empty lines with blanks, only). – Alex Kulinkovich Nov 30 '20 at 16:55
  • @Alex.K. that I understood, since it's already mentioned in the answers themselves. Sorry, should have been more specific: is there anything else in the white space but space and tab in `\h`? Also `\r?\n` already includes Win and Unix line breaks, whats the advantage of using `\R`? Are there "regular" use cases where you need it? – Albin Dec 01 '20 at 22:40
26

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

Saikat
  • 14,222
  • 20
  • 104
  • 125
20

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!
Kamal Nayan
  • 1,890
  • 21
  • 34
15

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

Bassem
  • 3,135
  • 2
  • 25
  • 41
15

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.

Praboda
  • 520
  • 6
  • 12
  • 1
    Please note: This does not only replace empty lines, it replaces e.g. indents (tab and space) as well – Albin Nov 30 '20 at 14:23
13

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

Ya Basha
  • 1,902
  • 6
  • 30
  • 54
13

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

Ahmad
  • 22,657
  • 9
  • 52
  • 84
11

Ctrl+H.

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

mahesh adepu
  • 111
  • 1
  • 2
  • This works for me, removing extra line, in the sense of finding double \r replace with single \r. Thanks!! – genpet Jun 03 '12 at 01:59
  • This worked well, however I found that I needed to run it several times in my document before all instances of \r\r were gone. I'm unsure why. – Hawkwing Aug 21 '13 at 16:59
7

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.

Poisson
  • 91
  • 1
  • 1
5

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...

Wasim A.
  • 9,660
  • 22
  • 90
  • 120
4
  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

andr
  • 15,970
  • 10
  • 45
  • 59
Adeeb
  • 49
  • 1
  • 1
4

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

4

/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.

AshBrad
  • 482
  • 2
  • 15
3

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
3

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
_____________________________________
slyfox1186
  • 301
  • 1
  • 13
2

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

Chris
  • 156
  • 1
  • 4
1

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.

poisson
  • 31
  • 2
1

this work for me:

SEARCH:^\r  
REPLACE:            (empty)
Jeremy W
  • 1,889
  • 6
  • 29
  • 37
Rafael Marques
  • 211
  • 2
  • 4