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

Copy the matched substring only with :g

Expand Messages
  • BPJ
    Is there any way when using :g to copy the matched substring only? I want to find all lines with CamelCase words and copy those words to the bottom of the
    Message 1 of 9 , Jan 15 6:14 AM
      Is there any way when using :g to copy the matched substring only?

      I want to find all lines with CamelCase words and copy those
      words to the bottom of the buffer.

      :g/\a*\l\U\a*/t$

      copies the whole line, but I want to get the part of the line
      matching the pattern.

      TIA,

      /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.
    • Paul Isambert
      ... Quick solution: g/ a* ( l u | u l ) a*/call setline(line( $ )+1, matchstr(getline( . ), a* ( l u | u l ) a* )) Notes: - I ve replaced U with u, since
      Message 2 of 9 , Jan 15 6:29 AM
        "BPJ" <bpj@...>:
        > Is there any way when using :g to copy the matched substring only?
        >
        > I want to find all lines with CamelCase words and copy those
        > words to the bottom of the buffer.
        >
        > :g/\a*\l\U\a*/t$
        >
        > copies the whole line, but I want to get the part of the line
        > matching the pattern.

        Quick solution:

        g/\a*\(\l\u\|\u\l\)\a*/call setline(line('$')+1, matchstr(getline('.'), '\a*\(\l\u\|\u\l\)\a*'))

        Notes:
        - I've replaced \U with \u, since \U means [^A-Z].
        - I've replaced \l\u with \(\l\u\|\u\l\), otherwise words starting with an
        uppercase letter won't be found.

        Best,
        Paul

        --
        --
        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
        ... Thanks! ... D oh I make that one all the time... ... Why so? Doesn t the a* take care of it? ... -- -- You received this message from the vim_use
        Message 3 of 9 , Jan 15 6:34 AM
          2014-01-15 15:29, Paul Isambert skrev:
          > "BPJ" <bpj@...>:
          >> Is there any way when using :g to copy the matched substring only?
          >>
          >> I want to find all lines with CamelCase words and copy those
          >> words to the bottom of the buffer.
          >>
          >> :g/\a*\l\U\a*/t$
          >>
          >> copies the whole line, but I want to get the part of the line
          >> matching the pattern.
          >
          > Quick solution:
          >
          > g/\a*\(\l\u\|\u\l\)\a*/call setline(line('$')+1, matchstr(getline('.'), '\a*\(\l\u\|\u\l\)\a*'))

          Thanks!

          > Notes:
          > - I've replaced \U with \u, since \U means [^A-Z].

          D'oh I make that one all the time...

          > - I've replaced \l\u with \(\l\u\|\u\l\), otherwise words starting with an
          > uppercase letter won't be found.

          Why so? Doesn't the \a* take care of it?

          >
          > Best,
          > Paul
          >

          --
          --
          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.
        • Paul Isambert
          ... No, there no way for a* l u a* to match on e.g. Initial : if the first a matches, then there is nothing left for u; if that a doesn t match, then
          Message 4 of 9 , Jan 15 6:42 AM
            > > - I've replaced \l\u with \(\l\u\|\u\l\), otherwise words starting
            > > with an uppercase letter won't be found.
            >
            > Why so? Doesn't the \a* take care of it?

            No, there no way for '\a*\l\u\a*' to match on e.g. "Initial": if the first \a
            matches, then there is nothing left for \u; if that \a doesn't match, then the
            first letter must match \l, and that won't work either.

            By the way, what I've sent you will only copy the first match of each line. If
            you wanted all camelcased words to be copied, it'll need further work.

            Best,
            Paul

            --
            --
            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
            ... Yeah, I realized right after I hit Send. ... No worries, I found a method which worked :-) ... In both cases I got a lot of cruft as well as what I looked
            Message 5 of 9 , Jan 15 7:05 AM
              2014-01-15 15:42, Paul Isambert skrev:
              >>> - I've replaced \l\u with \(\l\u\|\u\l\), otherwise words starting
              >>> with an uppercase letter won't be found.
              >>
              >> Why so? Doesn't the \a* take care of it?
              >
              > No, there no way for '\a*\l\u\a*' to match on e.g. "Initial": if the first \a
              > matches, then there is nothing left for \u; if that \a doesn't match, then the
              > first letter must match \l, and that won't work either.

              Yeah, I realized right after I hit Send.

              >
              > By the way, what I've sent you will only copy the first match of each line. If
              > you wanted all camelcased words to be copied, it'll need further work.

              No worries, I found a method which worked :-)

              :$!perl -nlE'say for /\pL*\p{Ll}\p{Lu}\pL*|\b\p{Lu}\p{Ll}*\b/g' %

              In both cases I got a lot of cruft as well as what I looked for,
              but at leas I could now `vip:sort u` and scan some 20 lines
              of hits instead of trying to spot them scanning the whole file...

              Thanks anyway!

              /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.
            • Tim Chase
              ... Riffing off Paul s solution, I d suggest ... which removes the need to specify the pattern twice, using the / register to specify it to matchstr. The
              Message 6 of 9 , Jan 15 11:50 AM
                On 2014-01-15 15:29, Paul Isambert wrote:
                > g/\a*\(\l\u\|\u\l\)\a*/call setline(line('$')+1,
                > matchstr(getline('.'), '\a*\(\l\u\|\u\l\)\a*'))


                Riffing off Paul's solution, I'd suggest

                :g/\<\w*\l\u\w*\>/call setline(line('$')+1, matchstr(getline('.'), @/))

                which removes the need to specify the pattern twice, using the "/"
                register to specify it to matchstr.

                The regexp can be tweaked (I chose the above as my "there's something
                camelCase" regexp), but there are cases you'd have to make a
                determination on camelCase'ness:

                HTMLobject
                MyObject
                _myProperty
                _MyObject
                InnerHTML
                THIS_IS_HTML

                -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.
              • BPJ
                ... Nice! I ll try to remember that. ... Yes, and it turned out some of my cC words were really plain old Titlecase, hence the second branch in the pattern of
                Message 7 of 9 , Jan 15 12:01 PM
                  2014-01-15 20:50, Tim Chase skrev:
                  > On 2014-01-15 15:29, Paul Isambert wrote:
                  >> g/\a*\(\l\u\|\u\l\)\a*/call setline(line('$')+1,
                  >> matchstr(getline('.'), '\a*\(\l\u\|\u\l\)\a*'))
                  >
                  >
                  > Riffing off Paul's solution, I'd suggest
                  >
                  > :g/\<\w*\l\u\w*\>/call setline(line('$')+1, matchstr(getline('.'), @/))
                  >
                  > which removes the need to specify the pattern twice, using the "/"
                  > register to specify it to matchstr.

                  Nice! I'll try to remember that.

                  >
                  > The regexp can be tweaked (I chose the above as my "there's something
                  > camelCase" regexp), but there are cases you'd have to make a
                  > determination on camelCase'ness:
                  >
                  > HTMLobject
                  > MyObject
                  > _myProperty
                  > _MyObject
                  > InnerHTML
                  > THIS_IS_HTML
                  >

                  Yes, and it turned out some of my cC words were really plain old
                  Titlecase, hence the second branch in the pattern of my perl
                  filter solution.

                  /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.
                • Christian Brabandt
                  Hi Tim! ... That still doesn t get all matches per line. Here is a different ... Use whatever pattern you need. Best, Christian -- Lehrsatz der Pioniere: Je
                  Message 8 of 9 , Jan 15 12:19 PM
                    Hi Tim!

                    On Mi, 15 Jan 2014, Tim Chase wrote:

                    > On 2014-01-15 15:29, Paul Isambert wrote:
                    > > g/\a*\(\l\u\|\u\l\)\a*/call setline(line('$')+1,
                    > > matchstr(getline('.'), '\a*\(\l\u\|\u\l\)\a*'))
                    >
                    >
                    > Riffing off Paul's solution, I'd suggest
                    >
                    > :g/\<\w*\l\u\w*\>/call setline(line('$')+1, matchstr(getline('.'), @/))
                    >
                    > which removes the need to specify the pattern twice, using the "/"
                    > register to specify it to matchstr.
                    >
                    > The regexp can be tweaked (I chose the above as my "there's something
                    > camelCase" regexp), but there are cases you'd have to make a
                    > determination on camelCase'ness:
                    >
                    > HTMLobject
                    > MyObject
                    > _myProperty
                    > _MyObject
                    > InnerHTML
                    > THIS_IS_HTML

                    That still doesn't get all matches per line. Here is a different
                    approach (that needs some 7.3.600 patch or so):

                    :let a=[]
                    :%s/pattern/\=add(a,submatch(0))/gn
                    :call append('$', a)

                    Use whatever pattern you need.

                    Best,
                    Christian
                    --
                    Lehrsatz der Pioniere:
                    Je schwerer ein Baumstamm, desto größer sein Gewicht.

                    --
                    --
                    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
                    ... Nice. To get it to work in pre-7.3.600 (I believe you re talking about the evaluate the replacement with the n flag discussion from a while back), you
                    Message 9 of 9 , Jan 18 6:55 AM
                      On 2014-01-15 21:19, Christian Brabandt wrote:
                      > Hi Tim!
                      >
                      > On Mi, 15 Jan 2014, Tim Chase wrote:
                      >
                      > > On 2014-01-15 15:29, Paul Isambert wrote:
                      > > > g/\a*\(\l\u\|\u\l\)\a*/call setline(line('$')+1,
                      > > > matchstr(getline('.'), '\a*\(\l\u\|\u\l\)\a*'))
                      > >
                      > >
                      > > Riffing off Paul's solution, I'd suggest
                      > >
                      > > :g/\<\w*\l\u\w*\>/call setline(line('$')+1,
                      > > matchstr(getline('.'), @/))
                      > >
                      > > which removes the need to specify the pattern twice, using the "/"
                      > > register to specify it to matchstr.
                      > >
                      > > The regexp can be tweaked (I chose the above as my "there's
                      > > something camelCase" regexp), but there are cases you'd have to
                      > > make a determination on camelCase'ness:
                      > >
                      > > HTMLobject
                      > > MyObject
                      > > _myProperty
                      > > _MyObject
                      > > InnerHTML
                      > > THIS_IS_HTML
                      >
                      > That still doesn't get all matches per line. Here is a different
                      > approach (that needs some 7.3.600 patch or so):
                      >
                      > :let a=[]
                      > :%s/pattern/\=add(a,submatch(0))/gn
                      > :call append('$', a)
                      >
                      > Use whatever pattern you need.

                      Nice. To get it to work in pre-7.3.600 (I believe you're talking
                      about the "evaluate the replacement with the 'n' flag" discussion
                      from a while back), you can use

                      :let a=[]
                      :%s/pattern/\=empty(add(a, submatch(0)))?'':submatch(0)/g

                      It will alter 'modified', but at least it should be a noop for your
                      document.

                      -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.
                    Your message has been successfully submitted and would be delivered to recipients shortly.