Thursday, August 25, 2016

Re: Is there way to show search matches in a separate window similar to Acrobat?

On Friday, August 19, 2016 at 8:43:12 PM UTC-5, Peng Yu wrote:
> On Fri, Aug 19, 2016 at 6:19 PM, Erik Falor <ewfalor@gmail.com> wrote:
> > On Fri, Aug 19, 2016 at 05:35:48PM -0500, Peng Yu wrote:
> >> Hi, The attached image shows what is available in Acrobat, where all
> >> the search results will show in one window with search keyword
> >> highlighted. This can be convenient as one can easily see the search
> >> results across all the document and can jump to the hit easily by
> >> clicking.
> >>
> >> Is there something similar in vim?
> >
> > You may try
> >
> > :vimgrep /pattern/ % | copen
> >
> > or
> >
> > :lvimgrep /pattern/ % | lopen
> >
> > The '%' refers to the current file. You may use * and ** to refer to
> > many files at once (recursively).
>
> Thanks. Is there a way to simultaneously highlight all the matches in
> the newly opened window?
>
>

Sorry for the drive-by post, but I threw this together because you reminded me I've wanted the same thing.

Put the following in ~/.vim/ftplugin/qf.vim:

""" begin """
function! QfHlMatch()
if exists('w:quickfix_title') && w:quickfix_title =~ 'vimgrep'
if exists('b:QF_HL_id')
call matchdelete(b:QF_HL_id)
endif
let pat = substitute(w:quickfix_title, '\v.*vimgrep\s+(.)(%(%(\1)@!.|\\@<=\1)+)\1.*', '\2', '')
let b:QF_HL_id = matchadd('IncSearch', pat)
endif
endfun

augroup QF_HL
au! * <buffer>
au WinEnter <buffer> call QfHlMatch()
augroup END

call QfHlMatch()
""" end """

It uses the quickfix window title to extract the pattern used for the search. There may be a better way to get that.

If you need help understanding what's going on in that regex, consult the :help for:

/\%(
/\@<=
/\@!

etc.

The purpose of the regex is to strip out everything before the vimgrep pattern, delimited by an arbitrary character, keep up to the first non-escaped delimiter character, and strip out everything after. I've noticed it may do weird things if you use certain weird delimiters in certain cases but it mostly seems to work. There may be a better way.

I used IncSearch rather than Search because the current line in the quickfix list is already highlighted with Search. If you have different highlighting defined for each of those you'll be able to see the match even on the current line.

Matches update whenever you enter the quickfix list window. You could use CursorHold or similar if you want it to update automatically without going to the window.

--
--
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@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: