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.
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityYou 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'
:windo %s/pattern/replace/ge
, which searches & replaces in all windows 👍vim /pattern/ ##
for all the results in quickfix