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

how to search backwards for consecutive lines

Expand Messages
  • Christian Brabandt
    (resent, because the original message seems to got lost) Hi, suppose you have the following file: ,---- ... `---- If the cursor is on line 1, I can jump to the
    Message 1 of 3 , Apr 24 2:54 AM
    View Source
    • 0 Attachment
      (resent, because the original message seems to got lost)

      Hi,
      suppose you have the following file:

      ,----
      | 1
      | 2foo
      | 3foo
      | 4foo
      | 5foo
      | 6
      | 7
      | 8
      `----

      If the cursor is on line 1, I can jump to the end of the "foo" block by
      using:
      /^\(foo\n\)\+/e

      Is there a similar way when searching backwards (e.g. the cursor is on
      line 8 and I want to jump to beginning of line 2)?
      ?^\(foo\n\)\+ unfortunately jumps to the beginning of line 5. Can I make
      Vim somehow behave consistent to the forwarding search?


      regards,
      Christian

      --
      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php

      ---
      You received this message because you are subscribed to the Google Groups "vim_use" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • LCD 47
      ... This seems to work for your particular example, but I m not sure it is what you re looking for: ?^ ( nfoo ) +?+ /lcd -- -- You received this message from
      Message 2 of 3 , Apr 24 3:19 AM
      View Source
      • 0 Attachment
        On 24 April 2013, Christian Brabandt <cblists@...> wrote:
        > (resent, because the original message seems to got lost)
        >
        > Hi,
        > suppose you have the following file:
        >
        > ,----
        > | 1
        > | 2foo
        > | 3foo
        > | 4foo
        > | 5foo
        > | 6
        > | 7
        > | 8
        > `----
        >
        > If the cursor is on line 1, I can jump to the end of the "foo" block by
        > using:
        > /^\(foo\n\)\+/e
        >
        > Is there a similar way when searching backwards (e.g. the cursor is on
        > line 8 and I want to jump to beginning of line 2)?
        > ?^\(foo\n\)\+ unfortunately jumps to the beginning of line 5. Can I make
        > Vim somehow behave consistent to the forwarding search?

        This seems to work for your particular example, but I'm not sure it
        is what you're looking for:

        ?^\(\nfoo\)\+?+

        /lcd

        --
        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php

        ---
        You received this message because you are subscribed to the Google Groups "vim_use" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • John Little
        ... Search *patterns* match from a point then implicitly forward, even if the search is backward. So ?^ (foo n ) + matches on line 5, the pattern doesn t
        Message 3 of 3 , Apr 24 5:04 AM
        View Source
        • 0 Attachment
          On Wednesday, April 24, 2013 9:54:14 PM UTC+12, Christian Brabandt wrote:
          > ... somehow behave consistent to the forwarding search?

          Search *patterns* match from a point then implicitly forward, even if the search is backward. So ?^\(foo\n\)\+ matches on line 5, the pattern doesn't cross lines backwards.

          You can chain searches together with ; though. To search backwards for a line beginning with foo, then backwards for a line not beginning with foo, then forward one line:

          ?^foo?;?^\(foo\)\@!?+

          That doesn't work if the foos start on line 1, the + offset won't wrap around the file end or beginning, (this appears to me to be undocumented). If that's a problem,

          ?^foo?;?^\(foo\)\@!?;/^foo

          will work ('ws' being on). Unfortunately, back references don't reach across the ;, so if your "foo" was long, you might want to use a variable:

          :let p = "foo"
          :exe '?^' . p . '?;?^\(' . p . '\)\@!?;/^' . p

          HTH, John Little

          --
          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php

          ---
          You received this message because you are subscribed to the Google Groups "vim_use" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
          For more options, visit https://groups.google.com/groups/opt_out.
        Your message has been successfully submitted and would be delivered to recipients shortly.