Merge multiple blank lines into a single blank line?

  • I used to have a macro in another editor that allowed me to remove extra blank lines, i.e. when there are more than one blank lines in a row, just leave a single blank line. I can see in Notepad that it’s possible to remove all blank lines, but that’s not really what I want. Also it doesn’t seem that search and replace can deal with more than a single line at a time. What I’d like to do is replace \n\n with \n, but even when there are clearly multiple blank lines with no additional whitespace, the seach term \n\n is not found. How would I go about doing this in notepad++?

  • @Gabor-Szakacs

    Try this:

    Find what zone: (\R){3,}
    Replace with zone: \1\1
    Search mode: Regular expression

    Actually, extended search mode may work with \n but it will only find \n\n if your files are Unix (Linux) formatted and two \n occur back-to-back. You probably have Windows formatted files (with \r\n line-endings), so a back-to-back double blank line would look like \r\n\r\n.

  • @Gabor-Szakacs

    it doesn’t seem that search and replace can deal with more than a single line at a time

    It can; this has been discussed before. The easiest way to do a literal search for multi-line text is to select some multi-line data before invoking Find (e.g. with ctrl+f). You will end up with what looks like two (or more) lines running together in the Find what zone (if your lines are short enough to be seen at one time in the box). However, where the lines appear to run together will be an invisible character that actually is the line-ending data–you can “feel” this by using the arrow keys to move over this character–you will find it takes an extra arrow press to get over it than what your eyes tell you.

  • @Gabor-Szakacs

    …and there is a nice script solution by @Claudia-Frank for easily getting multi-line data in both the Find what and Replace with zones in this thread.

  • Hello, @gabor-szakacs, @scott-sumner, and All,

    I thought about an other solution, similar to the Scott’s one, but taking in account :

    • Possible leading blank characters, in empty lines

    • Possible trailing blank characters, in non-empty lines

    So :

    SEARCH (\h*\R){3,}|\h+\R

    REPLACE \r\n(?1\r\n)

    • Tick the Wrap around option

    • Select the Regular expression search mode

    • Click once the Replace All button or several times on the Replace button

    Remark :

    If you apply this regex S/R on Unix files, simply change the Replace regex from \r\n(?1\r\n) to \n(?1\n)

    So, in brief, this S/R will normalize any gap between paragraphs, to a single empty line as a separator and delete, as well, any trailing blank characters



  • @guy038

    If you apply this regex S/R on Unix files, simply change the…

    Just a little note…in these cases I like to capture the \R into a numbered group to use at replace time…that way the expressions can be agnostic of whether the file format is Unix or Windows.

  • Hello, @gabor-szakacs, @scott-sumner, and All,

    Indeed, Scott, I thought about capturing \R ! But, as my regex have two alternatives, when it matches \h+\R, the group 2 ( = \R ), in the first alternative does not exist. Thus, I need to capture \R in the second alternative, too ( group 3 )

    So :

    SEARCH (\h*(\R)){3,}|\h+(\R)

    REPLACE (?1\2\2:\3)

    And, of course, the conditional replacement rewrites :

    • Two line-breaks \2\2, if the first alternative is found

    • One line-break \3, if the second alternative is found

    However, to my mind, this second syntax does not look as elegant as my previous one ;-))



    BTW, my previous regex S/R may be slightly shortened as :

    SEARCH (\h*\R){3,}|\h+\R

    REPLACE \r\n?1\r\n ( or \n?1\n on UNIX files )

Log in to reply