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

How to Move/Copy multi-line search matches?

Expand Messages
  • Sibin Thomas
    Hi, I have a multi-line search pattern ( * cTable s* d +: _. {-} d + s* n ze. {-} % 75c.**) which matches multiple text segments in a file. E.g. : One of the
    Message 1 of 10 , Jan 26, 2014
    • 0 Attachment
      Hi,

      I have a multi-line search pattern (\cTable\s*\d\+:\_.\{-}\d\+\s*\n\ze.\{-}\%>75c.*) which matches multiple text segments in a file. 
      E.g. : One of the matched text segments - 

      Inline image 1

      I would like to know how all the matched segments can be moved or copied via a single operation. 

      I am looking for something similar to 1) :g/<pattern>/t$ 2) :g/<pattern>/m$ 3) :g/<pattern>/y A, except that it should work for the entire multi-line text segment and not just the first line.

      --
      Regards,
      Sibin

      --
      --
      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
      How about using //e to extend a visual selection? Say the current search is your multi-line pattern, then ... appends the text of each match into register a,
      Message 2 of 10 , Jan 27, 2014
      • 0 Attachment
        How about using //e to extend a visual selection?

        Say the current search is your multi-line pattern, then

        :let @a=""
        :g//exe "normal v//e\<cr>$\"Ay"

        appends the text of each match into register a, as whole lines (thanks to the $).

        Regards, 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.
      • BPJ
        ... Why the backslash before ? Will without it be replaced by ^M in a doublequoted string? Where in the :help is that? And if so why not a
        Message 3 of 10 , Jan 27, 2014
        • 0 Attachment
          2014-01-27 09:26, John Little skrev:
          > How about using //e to extend a visual selection?
          >
          > Say the current search is your multi-line pattern, then
          >
          > :let @a=""
          > :g//exe "normal v//e\<cr>$\"Ay"
          >
          > appends the text of each match into register a, as whole lines (thanks to the $).

          Why the backslash before <cr>? Will <cr> without it be replaced
          by ^M in a doublequoted string? Where in the :help is that?
          And if so why not a singlequoted string here anyway?

          Just trying to learn.

          /bpj

          --
          --
          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
          ... Because... ... No. I suppose r would be shorter, but in vim when to use r or n is confusing. ... one can use single-quotes, and type a ^M by pressing
          Message 4 of 10 , Jan 27, 2014
          • 0 Attachment
            On Monday, January 27, 2014 10:13:35 PM UTC+13, BPJ wrote:

            > Why the backslash before <cr>?

            Because...
            > Will <cr> without it be replaced
            > by ^M in a doublequoted string?

            No. I suppose \r would be shorter, but in vim when to use \r or \n is confusing.

            > Where in the :help is that?

            :help expr-quote

            > And if so why not a singlequoted string here anyway?

            one can use single-quotes, and type a ^M by pressing ctrl-V first, but for the purposes of a group post, confusion can result when various web browsers and mail clients mangle the non-printing character. Using "\<cr>" avoids that and also is plain to human readers what is intended. In fact no quotes are needed, and the execute is not needed, I just tried

            :g//normal v//e^M$"Ay

            where ^M appears I typed Control-V and pressed enter. Simpler, though, to give a command that readers can copy and paste.

            Regards, 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.
          • Tim Chase
            ... Because you know that it will have at least the two lines, it simplifies matters a bit. You can specify a range to the command that :g executes,
            Message 5 of 10 , Jan 27, 2014
            • 0 Attachment
              On 2014-01-27 11:25, Sibin Thomas wrote:
              > I have a multi-line search pattern (
              > *\cTable\s*\d\+:\_.\{-}\d\+\s*\n\ze.\{-}\%>75c.**) which matches
              > multiple text segments in a file.
              > E.g. : One of the matched text segments -
              >
              > [image: Inline image 1]
              >
              > I would like to know how all the matched segments can be moved or
              > copied via a single operation.
              >
              > I am looking for something similar to 1)* :g/<pattern>/t$* 2)
              > *:g/<pattern>/m$ *3) *:g/<pattern>/y A,* except that it should work
              > for the entire multi-line text segment and not just the first line.

              Because you know that it will have at least the two lines, it
              simplifies matters a bit. You can specify a range to the command
              that :g executes, something like

              :g/{pattern}/{range}t

              which would make your command something like

              :g/\cTable\s*\s*\d\+:\_.\{-}\d\+\s*\n\ze.\{-}\%>75c.*/.;/\ze.\{-}\%>75c/t$

              \__________pattern_______________________________/ \_____range_____/

              -tim





              --
              --
              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.
            • Ben Fritz
              ... I like this approach, but I wouldn t even bother with visual mode. I d just use the gn object introduced in Vim 7.4. So instead of exe it would be normal
              Message 6 of 10 , Jan 27, 2014
              • 0 Attachment
                On Monday, January 27, 2014 2:26:05 AM UTC-6, John Little wrote:
                > How about using //e to extend a visual selection?
                >
                >
                >
                > Say the current search is your multi-line pattern, then
                >
                >
                >
                > :let @a=""
                >
                > :g//exe "normal v//e\<cr>$\"Ay"
                >
                >
                >
                > appends the text of each match into register a, as whole lines (thanks to the $).
                >
                >

                I like this approach, but I wouldn't even bother with visual mode. I'd just use the gn object introduced in Vim 7.4. So instead of exe it would be 'normal "Aygn' to yank the entire match and append to the A register.

                --
                --
                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.
              • Sibin Thomas
                Thanks for the reply John. This method isn t good enough for my use case since I want to copy all of the matches simultaneously, not just one of them. However,
                Message 7 of 10 , Jan 27, 2014
                • 0 Attachment
                  Thanks for the reply John.
                  This method isn't good enough for my use case since I want to copy all
                  of the matches simultaneously, not just one of them.

                  However, this method will work when used as part of a macro and is
                  something which I already used.
                  I was looking for a single step method.

                  On Mon, Jan 27, 2014 at 1:56 PM, John Little <John.B.Little@...> wrote:
                  > How about using //e to extend a visual selection?
                  >
                  > Say the current search is your multi-line pattern, then
                  >
                  > :let @a=""
                  > :g//exe "normal v//e\<cr>$\"Ay"
                  >
                  > appends the text of each match into register a, as whole lines (thanks to the $).
                  >
                  > Regards, 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.



                  --
                  Regards,
                  Sibin

                  --
                  --
                  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.
                • Sibin Thomas
                  Thanks for the tip Tim. This (below) worked for me - g/ cTable s* d +: _. {-} d + s* n ze. {-} % 75c.*/.,/ cTable s* d +: _. {-} d + s* n zs. {-} % 75c.*/-1m$
                  Message 8 of 10 , Jan 27, 2014
                  • 0 Attachment
                    Thanks for the tip Tim.

                    This (below) worked for me -
                    g/\cTable\s*\d\+:\_.\{-}\d\+\s*\n\ze.\{-}\%>75c.*/.,/\cTable\s*\d\+:\_.\{-}\d\+\s*\n\zs.\{-}\%>75c.*/-1m$

                    \_______________range________________/
                    range = current line[1] to the line preceding[3] the line which
                    matches the new search pattern[2]
                    [1] : .
                    [2] : \cTable\s*\d\+:\_.\{-}\d\+\s*\n\zs.\{-}\%>75c.*
                    [3] : -1

                    On Mon, Jan 27, 2014 at 5:48 PM, Tim Chase <vim@...> wrote:
                    > On 2014-01-27 11:25, Sibin Thomas wrote:
                    >> I have a multi-line search pattern (
                    >> *\cTable\s*\d\+:\_.\{-}\d\+\s*\n\ze.\{-}\%>75c.**) which matches
                    >> multiple text segments in a file.
                    >> E.g. : One of the matched text segments -
                    >>
                    >> [image: Inline image 1]
                    >>
                    >> I would like to know how all the matched segments can be moved or
                    >> copied via a single operation.
                    >>
                    >> I am looking for something similar to 1)* :g/<pattern>/t$* 2)
                    >> *:g/<pattern>/m$ *3) *:g/<pattern>/y A,* except that it should work
                    >> for the entire multi-line text segment and not just the first line.
                    >
                    > Because you know that it will have at least the two lines, it
                    > simplifies matters a bit. You can specify a range to the command
                    > that :g executes, something like
                    >
                    > :g/{pattern}/{range}t
                    >
                    > which would make your command something like
                    >
                    > :g/\cTable\s*\s*\d\+:\_.\{-}\d\+\s*\n\ze.\{-}\%>75c.*/.;/\ze.\{-}\%>75c/t$
                    >
                    > \__________pattern_______________________________/ \_____range_____/
                    >
                    > -tim
                    >
                    >
                    >
                    >
                    >



                    --
                    Regards,
                    Sibin

                    --
                    --
                    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.
                  • Ben Fritz
                    ... This should work to copy all of the matches. Why do you think it will only copy one? It does a :g command on the last search pattern, which will execute
                    Message 9 of 10 , Jan 27, 2014
                    • 0 Attachment
                      On Monday, January 27, 2014 8:48:47 AM UTC-6, Sibin wrote:
                      > On Mon, Jan 27, 2014 at 1:56 PM, John Little <John.B.Little@...> wrote:
                      > > How about using //e to extend a visual selection?
                      > >
                      > > Say the current search is your multi-line pattern, then
                      > >
                      > > :let @a=""
                      > > :g//exe "normal v//e\<cr>$\"Ay"
                      > >
                      > > appends the text of each match into register a, as whole lines (thanks to the $).
                      > >
                      > Thanks for the reply John.
                      > This method isn't good enough for my use case since I want to copy all
                      > of the matches simultaneously, not just one of them.
                      >
                      > However, this method will work when used as part of a macro and is
                      > something which I already used.
                      > I was looking for a single step method.
                      >

                      This should work to copy all of the matches. Why do you think it will only copy
                      one? It does a :g command on the last search pattern, which will execute for
                      every line that matches your search. The command it runs will copy the match,
                      APPENDING it to the 'a' register. So, after the command runs, all matched text
                      should be in the 'a' register and you can paste it with the normal-mode command:

                      "ap

                      --
                      --
                      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.
                    • Sibin Thomas
                      I am sorry, I misread John s solution. I tried it out now and it does (kind of) work. This was the exact command which worked for me though - ... Thanks for
                      Message 10 of 10 , Jan 27, 2014
                      • 0 Attachment
                        I am sorry, I misread John's solution.
                        I tried it out now and it does (kind of) work.

                        This was the exact command which worked for me though -
                        :g//exe "normal v//e+1\<C-CR>\"Ay")

                        Thanks for correcting me Ben.

                        On Mon, Jan 27, 2014 at 9:00 PM, Ben Fritz <fritzophrenic@...> wrote:
                        >
                        > On Monday, January 27, 2014 8:48:47 AM UTC-6, Sibin wrote:
                        >> On Mon, Jan 27, 2014 at 1:56 PM, John Little <John.B.Little@...> wrote:
                        >> > How about using //e to extend a visual selection?
                        >> >
                        >> > Say the current search is your multi-line pattern, then
                        >> >
                        >> > :let @a=""
                        >> > :g//exe "normal v//e\<cr>$\"Ay"
                        >> >
                        >> > appends the text of each match into register a, as whole lines (thanks to the $).
                        >> >
                        >> Thanks for the reply John.
                        >> This method isn't good enough for my use case since I want to copy all
                        >> of the matches simultaneously, not just one of them.
                        >>
                        >> However, this method will work when used as part of a macro and is
                        >> something which I already used.
                        >> I was looking for a single step method.
                        >>
                        >
                        > This should work to copy all of the matches. Why do you think it will only copy
                        > one? It does a :g command on the last search pattern, which will execute for
                        > every line that matches your search. The command it runs will copy the match,
                        > APPENDING it to the 'a' register. So, after the command runs, all matched text
                        > should be in the 'a' register and you can paste it with the normal-mode command:
                        >
                        > "ap
                        >
                        > --
                        > --
                        > 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.



                        --
                        Regards,
                        Sibin

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