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

Re: Suggestion: Highlight the search pattern in :g/.../# or :g/.../p

Expand Messages
  • Jürgen Krämer
    Hi again, ... it misses at least one feature: it only highlights the first match on a line. ... Found it: http://vim.wikia.com/wiki/VimTip1141 Regards, Jürgen
    Message 1 of 13 , Mar 21, 2013
    • 0 Attachment
      Hi again,

      Jürgen Krämer wrote:
      >
      > Axel Bender wrote:
      >> How about implementing a hightlighting of the pattern searched for in
      >> :g/.../ expressions? At times the resulting list is quite long and it
      >> is difficult to identify the pattern. Highlighting it would be of
      >> great benefit (at least for me...).
      >
      > the last time somebody asked this question I wrote this little function
      >
      > function! PrintWithHighlighting()
      > let line = getline('.')
      > let ms = match(line, @/)
      > let me = matchend(line, @/)
      >
      > if ms != -1
      > echohl none
      > echo strpart(line, 0, ms)
      > echohl Search
      > echon strpart(line, ms, me - ms)
      > echohl none
      > echon strpart(line, me, strlen(line))
      > else
      > echo line
      > endif
      > endfunction
      >
      > command! P call PrintWithHighlighting()
      >
      > You can use it like this
      >
      > :g/you pattern/P
      >
      > I have never tested it much and I think I did not get any feedback if
      > it behaved as wanted.

      it misses at least one feature: it only highlights the first match on a
      line.

      > In the same thread somebody else replied with suggesting a script that
      > probably can be found on vim.org and might be tested more.
      > Alas, I cannot find the original thread at the moment.

      Found it: http://vim.wikia.com/wiki/VimTip1141

      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_dev" 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_dev" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Barry Arthur
      ... function! PrintWithHighlighting() let line = getline( . ) let ms = match(line, @/) let me = matchend(line, @/) while ms != -1 echohl none echon
      Message 2 of 13 , Mar 21, 2013
      • 0 Attachment
        On Thursday, March 21, 2013 5:51:50 PM UTC+8, Axel Bender wrote:
        > How about implementing a hightlighting of the pattern searched for in :g/.../ expressions? At times the resulting list is quite long and it is difficult to identify the pattern. Highlighting it would be of great benefit (at least for me...).

        function! PrintWithHighlighting()
        let line = getline('.')
        let ms = match(line, @/)
        let me = matchend(line, @/)

        while ms != -1
        echohl none
        echon strpart(line, 0, ms)
        echohl Search
        echon strpart(line, ms, me - ms)
        echohl none
        let line = strpart(line, me)
        let ms = match(line, @/)
        let me = matchend(line, @/)
        endwhile
        echon line . "\n"
        endfunction

        command! P call PrintWithHighlighting()

        --
        --
        You received this message from the "vim_dev" 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_dev" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • Gary Johnson
        ... I ve been using that one for years, probably since it appeared in 2006. It works great. ... Regards, Gary -- -- You received this message from the
        Message 3 of 13 , Mar 21, 2013
        • 0 Attachment
          On 2013-03-21, Jürgen Krämer wrote:
          > Hi again,
          >
          > Jürgen Krämer wrote:

          > > In the same thread somebody else replied with suggesting a script that
          > > probably can be found on vim.org and might be tested more.
          > > Alas, I cannot find the original thread at the moment.
          >
          > Found it: http://vim.wikia.com/wiki/VimTip1141

          I've been using that one for years, probably since it appeared in
          2006. It works great.

          :g/pattern/PP

          Regards,
          Gary

          --
          --
          You received this message from the "vim_dev" 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_dev" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
          For more options, visit https://groups.google.com/groups/opt_out.
        • Axel Bender
          Thanks all! Based on your input my final version (taking into account vim s number setting): command! -nargs=? P :call PrintHighlighted( ) function!
          Message 4 of 13 , Mar 21, 2013
          • 0 Attachment
            Thanks all!

            Based on your input my final version (taking into account vim's number setting):

            command! -nargs=? P :call PrintHighlighted(<q-args>)

            function! PrintHighlighted(arg)
            echo ""

            if a:arg == "#" || &number
            let l:lnum = line(".")

            echohl LineNr
            echon " " . repeat(" ", len(line("$")) - strlen(l:lnum)) . l:lnum . " "
            echohl NONE
            endif

            let l:line = getline(".")
            let l:pos = 0

            while 1
            let l:ms = match(l:line, @/, l:pos)

            if l:ms == -1
            echon strpart(l:line, l:pos)
            return
            endif

            echon strpart(l:line, l:pos, l:ms - l:pos)

            let l:me = matchend(l:line, @/, l:pos)

            echohl MarkerBlue
            echon strpart(l:line, l:ms, l:me - l:ms)
            echohl NONE

            if l:pos == l:me
            echon strpart(l:line, l:me)
            return
            endif

            let l:pos = l:me
            endwhile
            endfunction

            --
            --
            You received this message from the "vim_dev" 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_dev" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
            For more options, visit https://groups.google.com/groups/opt_out.
          • Barry Arthur
            The final version I added to SearchParty: function! PrintWithHighlighting() range let lnum = a:firstline let lnum_len = len(line( $ )) for line in
            Message 5 of 13 , Mar 21, 2013
            • 0 Attachment
              The final version I added to SearchParty:

              function! PrintWithHighlighting() range
                let lnum = a:firstline
                let lnum_len = len(line('$'))
                for line in getline(a:firstline, a:lastline)
                  echohl LineNr
                  echon printf("%*s ", lnum_len, lnum)
                  echohl none
                  let lnum += 1

                  let ms = match(line, @/)
                  let me = matchend(line, @/)
                  while ms != -1
                    echohl none
                    echon strpart(line, 0, ms)
                    echohl Search
                    echon strpart(line, ms, me - ms)
                    echohl none
                    let line = strpart(line, me)
                    let ms = match(line, @/)
                    let me = matchend(line, @/)
                  endwhile
                  echon line . "\n"
                endfor
              endfunction

              command! -range P <line1>,<line2>call PrintWithHighlighting()



              On 22 March 2013 02:12, Axel Bender <axel.bender@...> wrote:
              Thanks all!

              Based on your input my final version (taking into account vim's number setting):

              command! -nargs=? P :call PrintHighlighted(<q-args>)

              function! PrintHighlighted(arg)
                 echo ""

                 if a:arg == "#" || &number
                    let l:lnum = line(".")

                    echohl LineNr
                    echon " " . repeat(" ", len(line("$")) - strlen(l:lnum)) . l:lnum . " "
                    echohl NONE
                 endif

                 let l:line = getline(".")
                 let l:pos  = 0

                 while 1
                    let l:ms = match(l:line, @/, l:pos)

                    if l:ms == -1
                       echon strpart(l:line, l:pos)
                       return
                    endif

                    echon strpart(l:line, l:pos, l:ms - l:pos)

                    let l:me = matchend(l:line, @/, l:pos)

                    echohl MarkerBlue
                    echon strpart(l:line, l:ms, l:me - l:ms)
                    echohl NONE

                    if l:pos == l:me
                       echon strpart(l:line, l:me)
                       return
                    endif

                    let l:pos = l:me
                 endwhile
              endfunction

              --
              --
              You received this message from the "vim_dev" 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 a topic in the Google Groups "vim_dev" group.
              To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_dev/V2vGo2CcHqU/unsubscribe?hl=en.
              To unsubscribe from this group and all its topics, send an email to vim_dev+unsubscribe@....
              For more options, visit https://groups.google.com/groups/opt_out.



              --
              --
              You received this message from the "vim_dev" 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_dev" group.
              To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
              For more options, visit https://groups.google.com/groups/opt_out.
               
               
            • Choi, Jin-yong
              How about using :grep when searching through the entire file? function! Grep(pattern) exe grep . a:pattern let @/ = substitute(a:pattern, / (.* )/.*$ ,
              Message 6 of 13 , Mar 21, 2013
              • 0 Attachment

                How about using ‘:grep’ when searching through the entire file?

                 

                 

                function! Grep(pattern)

                    exe 'grep ' . a:pattern

                    let @/ = substitute(a:pattern, '/\(.*\)/.*$', '\1', '')

                    copen

                endfunction

                command! -nargs=+   Grep    call Grep(<q-args>) | set hls

                 

                cnoreabbrev   grep    Grep<Space>//<Space>%<C-Left><C-Left><Right><C-r>=Eatchar('\s')<CR>

                 

                (for Eatchar(), :helpgrep Eatchar )

                 

                 

                I prefer Quickfix list more than ‘:print’ output. Just personal preference :)

                 

                 

                From: vim_dev@... [mailto:vim_dev@...] On Behalf Of Barry Arthur
                Sent: Friday, March 22, 2013 6:54 AM
                To: vim_dev@...
                Subject: Re: Suggestion: Highlight the search pattern in :g/.../# or :g/.../p

                 

                The final version I added to SearchParty:

                 

                function! PrintWithHighlighting() range

                  let lnum = a:firstline

                  let lnum_len = len(line('$'))

                  for line in getline(a:firstline, a:lastline)

                    echohl LineNr

                    echon printf("%*s ", lnum_len, lnum)

                    echohl none

                    let lnum += 1

                 

                    let ms = match(line, @/)

                    let me = matchend(line, @/)

                    while ms != -1

                      echohl none

                      echon strpart(line, 0, ms)

                      echohl Search

                      echon strpart(line, ms, me - ms)

                      echohl none

                      let line = strpart(line, me)

                      let ms = match(line, @/)

                      let me = matchend(line, @/)

                    endwhile

                    echon line . "\n"

                  endfor

                endfunction

                 

                command! -range P <line1>,<line2>call PrintWithHighlighting()

                 

                 

                On 22 March 2013 02:12, Axel Bender <axel.bender@...> wrote:

                Thanks all!

                Based on your input my final version (taking into account vim's number setting):

                command! -nargs=? P :call PrintHighlighted(<q-args>)

                function! PrintHighlighted(arg)
                   echo ""

                   if a:arg == "#" || &number
                      let l:lnum = line(".")

                      echohl LineNr
                      echon " " . repeat(" ", len(line("$")) - strlen(l:lnum)) . l:lnum . " "
                      echohl NONE
                   endif

                   let l:line = getline(".")
                   let l:pos  = 0

                   while 1
                      let l:ms = match(l:line, @/, l:pos)

                      if l:ms == -1
                         echon strpart(l:line, l:pos)
                         return
                      endif

                      echon strpart(l:line, l:pos, l:ms - l:pos)

                      let l:me = matchend(l:line, @/, l:pos)

                      echohl MarkerBlue
                      echon strpart(l:line, l:ms, l:me - l:ms)
                      echohl NONE

                      if l:pos == l:me
                         echon strpart(l:line, l:me)
                         return
                      endif

                      let l:pos = l:me
                   endwhile
                endfunction


                --
                --
                You received this message from the "vim_dev" 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 a topic in the Google Groups "vim_dev" group.
                To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_dev/V2vGo2CcHqU/unsubscribe?hl=en.
                To unsubscribe from this group and all its topics, send an email to vim_dev+unsubscribe@....
                For more options, visit https://groups.google.com/groups/opt_out.

                 

                --
                --
                You received this message from the "vim_dev" 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_dev" group.
                To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                For more options, visit https://groups.google.com/groups/opt_out.
                 
                 

                --
                --
                You received this message from the "vim_dev" 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_dev" group.
                To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                For more options, visit https://groups.google.com/groups/opt_out.
                 
                 
              • Christian Brabandt
                Hi Barry! ... I think, you might be getting problems, if your pattern match is zero-width, e.g. searching for :g/^/P might not work correctly. regards,
                Message 7 of 13 , Mar 22, 2013
                • 0 Attachment
                  Hi Barry!

                  On Fr, 22 Mär 2013, Barry Arthur wrote:

                  > The final version I added to SearchParty:
                  >
                  > function! PrintWithHighlighting() range
                  > let lnum = a:firstline
                  > let lnum_len = len(line('$'))
                  > for line in getline(a:firstline, a:lastline)
                  > echohl LineNr
                  > echon printf("%*s ", lnum_len, lnum)
                  > echohl none
                  > let lnum += 1
                  >
                  > let ms = match(line, @/)
                  > let me = matchend(line, @/)
                  > while ms != -1
                  > echohl none
                  > echon strpart(line, 0, ms)
                  > echohl Search
                  > echon strpart(line, ms, me - ms)
                  > echohl none
                  > let line = strpart(line, me)
                  > let ms = match(line, @/)
                  > let me = matchend(line, @/)
                  > endwhile
                  > echon line . "\n"
                  > endfor
                  > endfunction
                  >
                  > command! -range P <line1>,<line2>call PrintWithHighlighting()

                  I think, you might be getting problems, if your pattern match is
                  zero-width, e.g. searching for :g/^/P might not work correctly.

                  regards,
                  Christian
                  --

                  --
                  --
                  You received this message from the "vim_dev" 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_dev" group.
                  To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                  For more options, visit https://groups.google.com/groups/opt_out.
                • Barry Arthur
                  Ooops! Good catch. I think this fixes it: diff --git a/plugin/SearchParty.vim b/plugin/SearchParty.vim index 373ffd3..2b194f7 100644 ... +++
                  Message 8 of 13 , Mar 22, 2013
                  • 0 Attachment
                    Ooops! Good catch. I think this fixes it:

                    diff --git a/plugin/SearchParty.vim b/plugin/SearchParty.vim
                    index 373ffd3..2b194f7 100644
                    --- a/plugin/SearchParty.vim
                    +++ b/plugin/SearchParty.vim
                    @@ -282,7 +282,7 @@ function! PrintWithHighlighting() range
                     
                         let ms = match(line, @/)
                         let me = matchend(line, @/)
                    -    while ms != -1
                    +    while ms != -1 && ms != me
                           echohl none
                           echon strpart(line, 0, ms)
                           echohl Search



                    On 22 March 2013 15:03, Christian Brabandt <cblists@...> wrote:
                    Hi Barry!

                    On Fr, 22 Mär 2013, Barry Arthur wrote:

                    > The final version I added to SearchParty:
                    >
                    > function! PrintWithHighlighting() range
                    >   let lnum = a:firstline
                    >   let lnum_len = len(line('$'))
                    >   for line in getline(a:firstline, a:lastline)
                    >     echohl LineNr
                    >     echon printf("%*s ", lnum_len, lnum)
                    >     echohl none
                    >     let lnum += 1
                    >
                    >     let ms = match(line, @/)
                    >     let me = matchend(line, @/)
                    >     while ms != -1
                    >       echohl none
                    >       echon strpart(line, 0, ms)
                    >       echohl Search
                    >       echon strpart(line, ms, me - ms)
                    >       echohl none
                    >       let line = strpart(line, me)
                    >       let ms = match(line, @/)
                    >       let me = matchend(line, @/)
                    >     endwhile
                    >     echon line . "\n"
                    >   endfor
                    > endfunction
                    >
                    > command! -range P <line1>,<line2>call PrintWithHighlighting()

                    I think, you might be getting problems, if your pattern match is
                    zero-width, e.g. searching for :g/^/P might not work correctly.

                    regards,
                    Christian
                    --

                    --
                    --
                    You received this message from the "vim_dev" 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 a topic in the Google Groups "vim_dev" group.
                    To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_dev/V2vGo2CcHqU/unsubscribe?hl=en.
                    To unsubscribe from this group and all its topics, send an email to vim_dev+unsubscribe@....
                    For more options, visit https://groups.google.com/groups/opt_out.



                    --
                    --
                    You received this message from the "vim_dev" 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_dev" group.
                    To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                    For more options, visit https://groups.google.com/groups/opt_out.
                     
                     
                  • Choi, Jin-yong
                    ... to ... for ... First of all, I apologize for violating the mailing rule. I think my previous e-mail is somehow strange-looking unlike others . But I
                    Message 9 of 13 , Mar 22, 2013
                    • 0 Attachment
                      On March 21, 2013 6:52 PM, Axel Bender wrote:

                      > How about implementing a highlighting of the pattern searched for in
                      :g/.../
                      > expressions? At times the resulting list is quite long and it is difficult
                      to
                      > identify the pattern. Highlighting it would be of great benefit (at least
                      for
                      > me...).


                      First of all, I apologize for violating the "mailing rule."
                      I think my previous e-mail is somehow strange-looking unlike others'. But I
                      didn't recognize the problem. Sorry for the confusion.


                      I want to suggest to use ':grep' for searching the text and highlighting
                      them.
                      Maybe ':grep' is not in your interest. But it could make something more
                      convenient cause we can get the result in Quickfix list.

                      Here is the code. Tested only on Windows platform and vim version is
                      currently 7.3.875.

                      function! Grep(pattern)
                      exe 'grep ' . a:pattern
                      let @/ = substitute(a:pattern, '/\(.*\)/.*$', '\1', '')
                      copen
                      endfunction
                      command! -nargs=+ Grep call Grep(<q-args>) | set hls

                      cnoreabbrev grep
                      Grep<Space>//<Space>%<C-Left><C-Left><Right><C-r>=Eatchar('\s')<CR>


                      for Eatchar(), :helpgrep Eatchar


                      --
                      --
                      You received this message from the "vim_dev" 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_dev" group.
                      To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+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.