Loading ...
Sorry, an error occurred while loading the content.

delete N following blanked lines

Expand Messages
  • epanda
    HI, I would like to knon how to delete N blanked lines I have tried this with 8 lines but it deletes only the first blank line g/ (^ n ) {8}/d Thanks Epanda
    Message 1 of 12 , May 1, 2008
    • 0 Attachment
      HI,


      I would like to knon how to delete N blanked lines

      I have tried this with 8 lines but it deletes only the first blank
      line

      g/\(^\n\)\{8}/d

      Thanks
      Epanda
      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Tony Mechelynck
      ... Try (untested) ... or, to also delete lines containing only zero or more spaces or tabs, ... The above assumes that you want to start searching at the line
      Message 2 of 12 , May 1, 2008
      • 0 Attachment
        On 01/05/08 11:38, epanda wrote:
        > HI,
        >
        >
        > I would like to knon how to delete N blanked lines
        >
        > I have tried this with 8 lines but it deletes only the first blank
        > line
        >
        > g/\(^\n\)\{8}/d
        >
        > Thanks
        > Epanda

        Try (untested)

        :.+1s/^\n// 8

        or, to also delete lines containing only zero or more spaces or tabs,

        :.+1s/^\s*\n// 8

        The above assumes that you want to start searching at the line below the
        cursor. Modify the range to start elsewhere.

        Best regards,
        Tony.
        --
        " I changed my headlights the other day. I put in strobe lights
        instead! Now when I drive at night, it looks like everyone else is
        standing still ..."
        -- Steven Wright

        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • Ben Schmidt
        ... Yeah. The :g command runs for the first line only of multiline matches. Just ... I m baffled as to how you expect that to work, Tony. Can you explain the
        Message 3 of 12 , May 1, 2008
        • 0 Attachment
          Tony Mechelynck wrote:
          > On 01/05/08 11:38, epanda wrote:
          >> HI,
          >>
          >>
          >> I would like to knon how to delete N blanked lines
          >>
          >> I have tried this with 8 lines but it deletes only the first blank
          >> line
          >>
          >> g/\(^\n\)\{8}/d

          Yeah. The :g command runs for the first line only of multiline matches. Just
          modify the d part to delete 8 lines. I think:

          :g/\(^\n\)\{8}/.,.+7d

          > Try (untested)
          >
          > :.+1s/^\n// 8
          >
          > or, to also delete lines containing only zero or more spaces or tabs,
          >
          > :.+1s/^\s*\n// 8

          I'm baffled as to how you expect that to work, Tony. Can you explain the syntax as
          you understand it? I never heard of putting a count in that position in Vim
          before; thought it was a Sed or Awk thing, and even then, thought it did something
          different to this!

          Cheers,

          Ben.



          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • Tony Mechelynck
          ... This is at lines 543-546 of *change.txt* For Vim version 7.1. Last change: 2008 Jan 16 Best regards, Tony. -- What is the difference between a Turing
          Message 4 of 12 , May 1, 2008
          • 0 Attachment
            On 01/05/08 12:38, Ben Schmidt wrote:
            > Tony Mechelynck wrote:
            >> On 01/05/08 11:38, epanda wrote:
            >>> HI,
            >>>
            >>>
            >>> I would like to knon how to delete N blanked lines
            >>>
            >>> I have tried this with 8 lines but it deletes only the first blank
            >>> line
            >>>
            >>> g/\(^\n\)\{8}/d
            >
            > Yeah. The :g command runs for the first line only of multiline matches. Just
            > modify the d part to delete 8 lines. I think:
            >
            > :g/\(^\n\)\{8}/.,.+7d
            >
            >> Try (untested)
            >>
            >> :.+1s/^\n// 8
            >>
            >> or, to also delete lines containing only zero or more spaces or tabs,
            >>
            >> :.+1s/^\s*\n// 8
            >
            > I'm baffled as to how you expect that to work, Tony. Can you explain the syntax as
            > you understand it? I never heard of putting a count in that position in Vim
            > before; thought it was a Sed or Awk thing, and even then, thought it did something
            > different to this!
            >
            > Cheers,
            >
            > Ben.

            From ":help :s" (which you ought to have checked):

            > When [range] and [count] are omitted, replace in the
            > current line only.
            > When [count] is given, replace in [count] lines,
            > starting with the last line in [range].[...]


            This is at lines 543-546 of
            *change.txt* For Vim version 7.1. Last change: 2008 Jan 16


            Best regards,
            Tony.
            --
            What is the difference between a Turing machine and the modern
            computer? It's the same as that between Hillary's ascent of Everest
            and the establishment of a Hilton on its peak.

            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • Ben Schmidt
            ... I did! But I was blind to the count; I only saw the flags, followed the link to ... Knowing the syntax now, though, I think that solution is slightly
            Message 5 of 12 , May 1, 2008
            • 0 Attachment
              Tony Mechelynck wrote:
              > On 01/05/08 12:38, Ben Schmidt wrote:
              >> Tony Mechelynck wrote:
              >>> On 01/05/08 11:38, epanda wrote:
              >>>> HI,
              >>>>
              >>>>
              >>>> I would like to knon how to delete N blanked lines
              >>>>
              >>>> I have tried this with 8 lines but it deletes only the first blank
              >>>> line
              >>>>
              >>>> g/\(^\n\)\{8}/d
              >> Yeah. The :g command runs for the first line only of multiline matches. Just
              >> modify the d part to delete 8 lines. I think:
              >>
              >> :g/\(^\n\)\{8}/.,.+7d
              >>
              >>> Try (untested)
              >>>
              >>> :.+1s/^\n// 8
              >>>
              >>> or, to also delete lines containing only zero or more spaces or tabs,
              >>>
              >>> :.+1s/^\s*\n// 8
              >> I'm baffled as to how you expect that to work, Tony. Can you explain the syntax as
              >> you understand it? I never heard of putting a count in that position in Vim
              >> before; thought it was a Sed or Awk thing, and even then, thought it did something
              >> different to this!
              >>
              >> Cheers,
              >>
              >> Ben.
              >
              > From ":help :s" (which you ought to have checked):

              I did! But I was blind to the count; I only saw the flags, followed the link to
              :s_flags, and found nothing there, of course!

              Knowing the syntax now, though, I think that solution is slightly different to
              what the OP wanted in that it will replace the first 8 blank lines it finds, not
              all blocks of 8 consecutive blank lines.

              Ben.



              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            • Tony Mechelynck
              ... Then I misunderstood. I thought he wanted to remove the next eight possibly noncontiguous blank lines. To remove all blocks of 8 blank lines, try
              Message 6 of 12 , May 1, 2008
              • 0 Attachment
                On 01/05/08 13:09, Ben Schmidt wrote:
                > Tony Mechelynck wrote:
                >> On 01/05/08 12:38, Ben Schmidt wrote:
                >>> Tony Mechelynck wrote:
                >>>> On 01/05/08 11:38, epanda wrote:
                >>>>> HI,
                >>>>>
                >>>>>
                >>>>> I would like to knon how to delete N blanked lines
                >>>>>
                >>>>> I have tried this with 8 lines but it deletes only the first blank
                >>>>> line
                >>>>>
                >>>>> g/\(^\n\)\{8}/d
                >>> Yeah. The :g command runs for the first line only of multiline matches. Just
                >>> modify the d part to delete 8 lines. I think:
                >>>
                >>> :g/\(^\n\)\{8}/.,.+7d
                >>>
                >>>> Try (untested)
                >>>>
                >>>> :.+1s/^\n// 8
                >>>>
                >>>> or, to also delete lines containing only zero or more spaces or tabs,
                >>>>
                >>>> :.+1s/^\s*\n// 8
                >>> I'm baffled as to how you expect that to work, Tony. Can you explain the syntax as
                >>> you understand it? I never heard of putting a count in that position in Vim
                >>> before; thought it was a Sed or Awk thing, and even then, thought it did something
                >>> different to this!
                >>>
                >>> Cheers,
                >>>
                >>> Ben.
                >> From ":help :s" (which you ought to have checked):
                >
                > I did! But I was blind to the count; I only saw the flags, followed the link to
                > :s_flags, and found nothing there, of course!
                >
                > Knowing the syntax now, though, I think that solution is slightly different to
                > what the OP wanted in that it will replace the first 8 blank lines it finds, not
                > all blocks of 8 consecutive blank lines.
                >
                > Ben.

                Then I misunderstood. I thought he wanted to remove the next eight
                possibly noncontiguous blank lines.

                To remove all blocks of 8 blank lines, try (untested)

                :%s/^\%(\s*\n\){8}//

                Remove the \s* if nonempty blank lines must stay.


                Best regards,
                Tony.
                --
                BEDEVERE: How do you know so much about swallows?
                ARTHUR: Well you have to know these things when you're a king, you know.
                "Monty Python and the Holy Grail" PYTHON (MONTY)
                PICTURES LTD

                --~--~---------~--~----~------------~-------~--~----~
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
                -~----------~----~----~----~------~----~------~--~---
              • Tony Mechelynck
                On 01/05/08 13:24, Tony Mechelynck wrote: [...] ... Oops! :%s/^ %( s* n ) {8}// ... -- George Orwell was an optimist.
                Message 7 of 12 , May 1, 2008
                • 0 Attachment
                  On 01/05/08 13:24, Tony Mechelynck wrote:
                  [...]
                  > To remove all blocks of 8 blank lines, try (untested)
                  >
                  > :%s/^\%(\s*\n\){8}//

                  Oops! :%s/^\%(\s*\n\)\{8}//

                  >
                  > Remove the \s* if nonempty blank lines must stay.
                  >
                  >
                  > Best regards,
                  > Tony.
                  --
                  George Orwell was an optimist.

                  --~--~---------~--~----~------------~-------~--~----~
                  You received this message from the "vim_use" maillist.
                  For more information, visit http://www.vim.org/maillist.php
                  -~----------~----~----~----~------~----~------~--~---
                • John Beckett
                  ... I ve never seen count before either! Now that I have, I think a slightly more accurate statement is that ... will execute the substitute on the 8 lines
                  Message 8 of 12 , May 1, 2008
                  • 0 Attachment
                    Ben Schmidt wrote:
                    > Knowing the syntax now, though, I think that solution is
                    > slightly different to what the OP wanted in that it will
                    > replace the first 8 blank lines it finds, not all blocks
                    > of 8 consecutive blank lines.

                    I've never seen 'count' before either! Now that I have, I think a slightly more
                    accurate statement is that
                    :.+1s/^\n// 8

                    will execute the substitute on the 8 lines starting with the next line. If any of
                    those lines are blank, they will be deleted. Like using :g//, the deletion doesn't
                    affect what happens next.

                    It seems to me that this is the same as
                    :.+1,.+8s/^\n//

                    I wonder if the 'count' syntax arose because it gives a convenient way to substitute
                    on 8 lines, starting with the current line:
                    :s/red/blue/ 8

                    To the OP: Faced with this sort of problem, I muck about making a pattern that finds
                    what I want (in your case, 8 consecutive blank lines). The aim is to then to do
                    (replaces what I searched for last):
                    :%s//new text/g

                    If I can't think of a substitute that works as I want in a reasonable amount of
                    time, I would see if recording keys would get the job done. In this case, I could
                    easily delete 8 lines and it would probably be good enough (find next match; delete
                    8 lines; repeat).

                    John


                    --~--~---------~--~----~------------~-------~--~----~
                    You received this message from the "vim_use" maillist.
                    For more information, visit http://www.vim.org/maillist.php
                    -~----------~----~----~----~------~----~------~--~---
                  • John Beckett
                    ... I ve just done a test, using count 3 (not 8). ... 111 222 333 444 555 ... Cursor at beginning of 111 . ... 111 222 333 444 555 ... Conclusion: The command
                    Message 9 of 12 , May 1, 2008
                    • 0 Attachment
                      Tony Mechelynck wrote:
                      > I thought he wanted to remove the next
                      > eight possibly noncontiguous blank lines.

                      I've just done a test, using count 3 (not 8).

                      ---start before---
                      111

                      222

                      333

                      444

                      555
                      ---end---

                      Cursor at beginning of '111'.
                      Command:
                      :.+1s/^\n// 3

                      ---start after---
                      111
                      222
                      333

                      444

                      555
                      ---end---

                      Conclusion: The command substitutes on 'count' lines, starting with the next line.
                      It is the same as:
                      :.+1,.+3s/^\n//

                      John


                      --~--~---------~--~----~------------~-------~--~----~
                      You received this message from the "vim_use" maillist.
                      For more information, visit http://www.vim.org/maillist.php
                      -~----------~----~----~----~------~----~------~--~---
                    • Tony Mechelynck
                      ... Substitute on n lines starting with the next would be :.+1,.+7s/ etc. or even 8:s/ etc. (since 8: gets automagically converted to :.,.+7). Since when both
                      Message 10 of 12 , May 1, 2008
                      • 0 Attachment
                        On 01/05/08 13:44, John Beckett wrote:
                        > Ben Schmidt wrote:
                        >> Knowing the syntax now, though, I think that solution is
                        >> slightly different to what the OP wanted in that it will
                        >> replace the first 8 blank lines it finds, not all blocks
                        >> of 8 consecutive blank lines.
                        >
                        > I've never seen 'count' before either! Now that I have, I think a slightly more
                        > accurate statement is that
                        > :.+1s/^\n// 8
                        >
                        > will execute the substitute on the 8 lines starting with the next line. If any of
                        > those lines are blank, they will be deleted. Like using :g//, the deletion doesn't
                        > affect what happens next.

                        Substitute on n lines starting with the next would be :.+1,.+7s/ etc. or
                        even 8:s/ etc. (since 8: gets automagically converted to :.,.+7). Since
                        when both [range] and [count] are present, the counting starts at the
                        end of the range, I was assuming they weren't interchangeable.

                        >
                        > It seems to me that this is the same as
                        > :.+1,.+8s/^\n//
                        >
                        > I wonder if the 'count' syntax arose because it gives a convenient way to substitute
                        > on 8 lines, starting with the current line:
                        > :s/red/blue/ 8
                        >
                        > To the OP: Faced with this sort of problem, I muck about making a pattern that finds
                        > what I want (in your case, 8 consecutive blank lines). The aim is to then to do
                        > (replaces what I searched for last):
                        > :%s//new text/g
                        >
                        > If I can't think of a substitute that works as I want in a reasonable amount of
                        > time, I would see if recording keys would get the job done. In this case, I could
                        > easily delete 8 lines and it would probably be good enough (find next match; delete
                        > 8 lines; repeat).
                        >
                        > John

                        Best regards,
                        Tony.
                        --
                        "This is a country where people are free to practice their religion,
                        regardless of race, creed, color, obesity, or number of dangling
                        keys ..."

                        --~--~---------~--~----~------------~-------~--~----~
                        You received this message from the "vim_use" maillist.
                        For more information, visit http://www.vim.org/maillist.php
                        -~----------~----~----~----~------~----~------~--~---
                      • Gene Kwiecinski
                        ... Yeah, I had similar issues with deleting multiline patterns. Skip the d and use s/// instead. Seems the g just hits on the address, and the d
                        Message 11 of 12 , May 1, 2008
                        • 0 Attachment
                          >I would like to knon how to delete N blanked lines
                          >I have tried this with 8 lines but it deletes only the first blank
                          >line
                          >g/\(^\n\)\{8}/d

                          Yeah, I had similar issues with deleting multiline patterns. Skip the
                          'd' and use 's///' instead.

                          Seems the 'g' just hits on the address, and the 'd' deletes *just that
                          line*.

                          What you want is to explicitly substitute the found-pattern with
                          nothing.

                          --~--~---------~--~----~------------~-------~--~----~
                          You received this message from the "vim_use" maillist.
                          For more information, visit http://www.vim.org/maillist.php
                          -~----------~----~----~----~------~----~------~--~---
                        • Jürgen Krämer
                          Hi, ... Regards, Jürgen -- Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact
                          Message 12 of 12 , May 2, 2008
                          • 0 Attachment
                            Hi,

                            epanda wrote:
                            >
                            > I would like to knon how to delete N blanked lines
                            >
                            > I have tried this with 8 lines but it deletes only the first blank
                            > line
                            >
                            > g/\(^\n\)\{8}/d

                            the :delete command can take a count:

                            :g/^\n\{8\}/d 8

                            Regards,
                            Jürgen

                            --
                            Sometimes I think the surest sign that intelligent life exists elsewhere
                            in the universe is that none of it has tried to contact us. (Calvin)

                            --~--~---------~--~----~------------~-------~--~----~
                            You received this message from the "vim_use" maillist.
                            For more information, visit http://www.vim.org/maillist.php
                            -~----------~----~----~----~------~----~------~--~---
                          Your message has been successfully submitted and would be delivered to recipients shortly.