2

Sometimes I have two windows open and I search for a word that appears in both. Hitting n only cycles through the results in the active window.

It'd be really convenient to somehow jump to the next search result in all open windows.

2
  • Also recently discovered :windo %s/pattern/replace/ge, which searches & replaces in all windows 👍
    – Atav32
    Apr 6, 2018 at 0:10
  • While not an answer to the question, you could the files you want to search to the arglist, and then vim /pattern/ ## for all the results in quickfix
    – D. Ben Knoble
    Apr 6, 2018 at 7:32

1 Answer 1

1

You can use :vimgrep to do your search over the windows and use 'switchbuf' with a value of at least useopen.

execute 'vimgrep //j ' . join(map(range(1, winnr('$')), '"#".winbufnr(v:val)'), ' ')
set switchbuf+=useopen

Now you can use the quickfix list to jump between matches you searched via :cnext/:cprev (I suggest mapping these) or by using the quickfix window, :copen.

Personally, I can't really see myself using this kind of search. I typically need to search more files so I use :vimgrep or :grep (I use ripgrep as my 'grepprg'). I use my own :Cfilter command to grep/filter the quickfix list.

function! s:cfilter(list, bang, pat)
  let [bool, operator] = a:bang ? ['&&', '!~'] : ['||', '=~']
  let pat = a:pat =~ '^/.*/$' ? a:pat[1:-2] : a:pat
  let pat = pat == '' ? @/ : pat
  call call('set'.a:list.'list', [(filter(call('get'.a:list.'list', []), "bufname(v:val.bufnr) ".operator." pat ".bool." v:val.text ".operator." pat"))])
  echo 'Filtered list: '.(a:bang ? 'not ' : '').'matching '.a:pat.' ('.len(call('get'.a:list.'list', [])).' items)'
endfunction
command! -nargs=1 -bang Cfilter call <SID>cfilter('qf', <bang>0, <q-args>)
command! -nargs=1 -bang Lfilter call <SID>cfilter('loc', <bang>0, <q-args>)

:Cfilter foo will filter the list for only entries with foo and :Cfilter! foo will filter the list with only entries without foo.

For more helps see:

:h :vimgrep
:h :_#n
:h join()
:h map()
:h range()
:h winnr()
:h winbufnr()
:h v:val
:h :execute
:h 'swb'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.