• Login
Community
  • Login

Merge multiple blank lines into a single blank line?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
7 Posts 3 Posters 3.4k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G
    Gabor Szakacs
    last edited by Jun 4, 2018, 6:28 PM

    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++?

    S 3 Replies Last reply Jun 4, 2018, 7:10 PM Reply Quote 0
    • S
      Scott Sumner @Gabor Szakacs
      last edited by Scott Sumner Jun 4, 2018, 7:12 PM Jun 4, 2018, 7:10 PM

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

      1 Reply Last reply Reply Quote 1
      • S
        Scott Sumner @Gabor Szakacs
        last edited by Jun 4, 2018, 7:41 PM

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

        1 Reply Last reply Reply Quote 2
        • S
          Scott Sumner @Gabor Szakacs
          last edited by Jun 4, 2018, 7:48 PM

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

          1 Reply Last reply Reply Quote 1
          • G
            guy038
            last edited by guy038 Jun 4, 2018, 8:51 PM Jun 4, 2018, 8:37 PM

            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

            Cheers,

            guy038

            S 1 Reply Last reply Jun 4, 2018, 9:15 PM Reply Quote 2
            • S
              Scott Sumner @guy038
              last edited by Jun 4, 2018, 9:15 PM

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

              1 Reply Last reply Reply Quote 1
              • G
                guy038
                last edited by guy038 Jun 5, 2018, 1:40 AM Jun 5, 2018, 1:27 AM

                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 ;-))

                Cheers,

                guy038

                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 )

                1 Reply Last reply Reply Quote 2
                6 out of 7
                • First post
                  6/7
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors